on master help sign in
Add errors for record types, make some minor fixes to record expressions
38c2b40Evan Czaplicki
compiler/src/Reporting/Error/Syntax.hs+234−21
@@ -2284,12 +2284,12 @@ toRecordReport source context record startRow startCol =
2284 )
2284 )
2285
2285
2286 RecordField row col ->
2286 RecordField row col ->
2287− let
2288− surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2289− region = toRegion row col
2290− in
2291 case Code.whatIsNext source row col of
2287 case Code.whatIsNext source row col of
2292 Code.Keyword keyword ->
2288 Code.Keyword keyword ->
2289+ let
2290+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2291+ region = toWiderRegion row col (fromIntegral (length keyword))
2292+ in
2293 Report.Report "RESERVED WORD" region [] $
2293 Report.Report "RESERVED WORD" region [] $
2294 Code.toSnippet source surroundings (Just region)
2294 Code.toSnippet source surroundings (Just region)
2295 (
2295 (
@@ -2302,6 +2302,10 @@ toRecordReport source context record startRow startCol =
2302 )
2302 )
2303
2303
2304 _ ->
2304 _ ->
2305+ let
2306+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2307+ region = toRegion row col
2308+ in
2305 Report.Report "UNFINISHED RECORD" region [] $
2309 Report.Report "UNFINISHED RECORD" region [] $
2306 Code.toSnippet source surroundings (Just region)
2310 Code.toSnippet source surroundings (Just region)
2307 (
2311 (
@@ -2443,7 +2447,7 @@ addNoteForRecordError normalRecommendation =
2443 , "}"
2447 , "}"
2444 ]
2448 ]
2445 , D.reflow $
2449 , D.reflow $
2446− "Notice that nothing comes directly after a newline. Each line starts with some spaces. I\
2450+ "Notice that each line starts with some indentation. Usually two or four spaces. I\
2447 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
2451 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
2448 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
2452 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
2449 ]
2453 ]
@@ -2458,13 +2462,13 @@ addNoteForRecordIndentError normalRecommendation =
2458 "I may be confused by indentation. For example, if you are trying to define\
2462 "I may be confused by indentation. For example, if you are trying to define\
2459 \ a record across multiple lines, I recommend using this format:"
2463 \ a record across multiple lines, I recommend using this format:"
2460 , D.indent 4 $ D.vcat $
2464 , D.indent 4 $ D.vcat $
2461− [ "[ \"Alice\""
2465+ [ "{ name = \"Alice\""
2462− , ", \"Bob\""
2466+ , ", age = 42"
2463− , ", \"Chuck\""
2467+ , ", height = 1.75"
2464− , "]"
2468+ , "}"
2465 ]
2469 ]
2466 , D.reflow $
2470 , D.reflow $
2467− "Notice that nothing comes directly after a newline. Each line starts with some spaces. I\
2471+ "Notice that each line starts with some indentation. Usually two or four spaces. I\
2468 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
2472 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
2469 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
2473 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
2470 ]
2474 ]
@@ -3399,38 +3403,247 @@ toTRecordReport :: Code.Source -> TRecord -> Row -> Col -> Report.Report
3399toTRecordReport source record startRow startCol =
3403toTRecordReport source record startRow startCol =
3400 case record of
3404 case record of
3401 TRecordOpen row col ->
3405 TRecordOpen row col ->
3402− error "TODO TRecordOpen" row col startRow startCol
3406+ let
3407+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3408+ region = toRegion row col
3409+ in
3410+ case Code.whatIsNext source row col of
3411+ Code.Keyword keyword ->
3412+ Report.Report "RESERVED WORD" region [] $
3413+ Code.toSnippet source surroundings (Just region)
3414+ (
3415+ D.reflow $
3416+ "I just started parsing a record type, but I got stuck on this field name:"
3417+ ,
3418+ D.reflow $
3419+ "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
3420+ \ that is a reserved word. Try using a different name!"
3421+ )
3422+
3423+ _ ->
3424+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3425+ Code.toSnippet source surroundings (Just region)
3426+ (
3427+ D.reflow $
3428+ "I just started parsing a record type, but I got stuck here:"
3429+ ,
3430+ D.fillSep
3431+ ["Record","types","look","like",D.dullyellow "{ name : String, age : Int },"
3432+ ,"so","I","was","expecting","to","see","a","field","name","next."
3433+ ]
3434+ )
3403
3435
3404 TRecordEnd row col ->
3436 TRecordEnd row col ->
3405− error "TODO TRecordEnd" row col
3437+ let
3438+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3439+ region = toRegion row col
3440+ in
3441+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3442+ Code.toSnippet source surroundings (Just region)
3443+ (
3444+ D.reflow $
3445+ "I am partway through parsing a record type, but I got stuck here:"
3446+ ,
3447+ D.stack
3448+ [ D.fillSep
3449+ ["I","was","expecting","to","see","a","closing","curly","brace","before","this,"
3450+ ,"so","try","adding","a",D.dullyellow "}","and","see","if","that","helps?"
3451+ ]
3452+ , D.toSimpleNote $
3453+ "When I get stuck like this, it usually means that there is a missing parenthesis\
3454+ \ or bracket somewhere earlier. It could also be a stray keyword or operator."
3455+ ]
3456+ )
3406
3457
3407 TRecordField row col ->
3458 TRecordField row col ->
3408− error "TODO TRecordField" row col
3459+ case Code.whatIsNext source row col of
3460+ Code.Keyword keyword ->
3461+ let
3462+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3463+ region = toWiderRegion row col (fromIntegral (length keyword))
3464+ in
3465+ Report.Report "RESERVED WORD" region [] $
3466+ Code.toSnippet source surroundings (Just region)
3467+ (
3468+ D.reflow $
3469+ "I am partway through parsing a record type, but I got stuck on this field name:"
3470+ ,
3471+ D.reflow $
3472+ "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
3473+ \ that is a reserved word. Try using a different name!"
3474+ )
3475+
3476+ _ ->
3477+ let
3478+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3479+ region = toRegion row col
3480+ in
3481+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3482+ Code.toSnippet source surroundings (Just region)
3483+ (
3484+ D.reflow $
3485+ "I am partway through parsing a record type, but I got stuck after that last comma:"
3486+ ,
3487+ addNoteForRecordTypeError $
3488+ D.reflow $
3489+ "Trailing commas are not allowed in records, so I was expecting to see another\
3490+ \ record field defined next. If you are done defining the record, the fix may\
3491+ \ be to delete that last comma?"
3492+ )
3409
3493
3410 TRecordColon row col ->
3494 TRecordColon row col ->
3411− error "TODO TRecordColon" row col
3495+ let
3496+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3497+ region = toRegion row col
3498+ in
3499+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3500+ Code.toSnippet source surroundings (Just region)
3501+ (
3502+ D.reflow $
3503+ "I am partway through parsing a record type, but I got stuck here:"
3504+ ,
3505+ addNoteForRecordTypeError $
3506+ D.fillSep $
3507+ ["I","just","saw","a","field","name,","so","I","was","expecting","to","see"
3508+ ,"a","colon","next.","So","try","putting","an",D.green ":","sign","here?"
3509+ ]
3510+ )
3412
3511
3413 TRecordType tipe row col ->
3512 TRecordType tipe row col ->
3414− error "TODO TRecordType" tipe row col
3513+ toTypeReport source tipe row col
3415
3514
3416 TRecordSpace space row col ->
3515 TRecordSpace space row col ->
3417 toSpaceReport source space row col
3516 toSpaceReport source space row col
3418
3517
3419 TRecordIndentOpen row col ->
3518 TRecordIndentOpen row col ->
3420− error "TODO TRecordIndentOpen" row col
3519+ let
3520+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3521+ region = toRegion row col
3522+ in
3523+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3524+ Code.toSnippet source surroundings (Just region)
3525+ (
3526+ D.reflow $
3527+ "I just saw the opening curly brace of a record type, but then I got stuck here:"
3528+ ,
3529+ addNoteForRecordTypeIndentError $
3530+ D.fillSep $
3531+ ["I","am","expecting","a","record","like",D.dullyellow "{ name : String, age : Int }","here."
3532+ ,"Try","defining","some","fields","of","your","own?"
3533+ ]
3534+ )
3535+
3536+ TRecordIndentEnd row col ->
3537+ let
3538+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3539+ region = toRegion row col
3540+ in
3541+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3542+ Code.toSnippet source surroundings (Just region)
3543+ (
3544+ D.reflow $
3545+ "I was expecting to see a closing curly brace next:"
3546+ ,
3547+ addNoteForRecordTypeIndentError $
3548+ D.fillSep $
3549+ ["Try","putting","an",D.green "}","and","see","if","that","helps?"
3550+ ]
3551+ )
3421
3552
3422 TRecordIndentField row col ->
3553 TRecordIndentField row col ->
3423− error "TODO TRecordIndentField" row col
3554+ let
3555+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3556+ region = toRegion row col
3557+ in
3558+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3559+ Code.toSnippet source surroundings (Just region)
3560+ (
3561+ D.reflow $
3562+ "I am partway through parsing a record type, but I got stuck after that last comma:"
3563+ ,
3564+ addNoteForRecordTypeIndentError $
3565+ D.reflow $
3566+ "Trailing commas are not allowed in record types, so the fix may be to\
3567+ \ delete that last comma? Or maybe you were in the middle of defining\
3568+ \ an additional field?"
3569+ )
3424
3570
3425 TRecordIndentColon row col ->
3571 TRecordIndentColon row col ->
3426− error "TODO TRecordIndentColon" row col
3572+ let
3573+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3574+ region = toRegion row col
3575+ in
3576+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3577+ Code.toSnippet source surroundings (Just region)
3578+ (
3579+ D.reflow $
3580+ "I am partway through parsing a record type. I just saw a record\
3581+ \ field, so I was expecting to see a colon next:"
3582+ ,
3583+ addNoteForRecordTypeIndentError $
3584+ D.fillSep $
3585+ ["Try","putting","an",D.green ":","followed","by","a","type?"
3586+ ]
3587+ )
3427
3588
3428 TRecordIndentType row col ->
3589 TRecordIndentType row col ->
3429− error "TODO TRecordIndentType" row col
3590+ let
3591+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3592+ region = toRegion row col
3593+ in
3594+ Report.Report "UNFINISHED RECORD TYPE" region [] $
3595+ Code.toSnippet source surroundings (Just region)
3596+ (
3597+ D.reflow $
3598+ "I am partway through parsing a record type, and I was expecting to run into a type next:"
3599+ ,
3600+ addNoteForRecordTypeIndentError $
3601+ D.fillSep $
3602+ ["Try","putting","something,","like"
3603+ ,D.dullyellow "Int","or",D.dullyellow "String","for","now?"
3604+ ]
3605+ )
3430
3606
3431− TRecordIndentEnd row col ->
3432− error "TODO TRecordIndentEnd" row col
3433
3607
3608+addNoteForRecordTypeError :: D.Doc -> D.Doc
3609+addNoteForRecordTypeError normalRecommendation =
3610+ D.stack $
3611+ normalRecommendation
3612+ :
3613+ [ D.toSimpleNote
3614+ "If you are trying to define a record type across multiple lines, I recommend using this format:"
3615+ , D.indent 4 $ D.vcat $
3616+ [ "{ name : String"
3617+ , ", age : Int"
3618+ , ", height : Float"
3619+ , "}"
3620+ ]
3621+ , D.reflow $
3622+ "Notice that each line starts with some indentation. Usually two or four spaces. I\
3623+ \ know this style can be jarring for people coming from C-like syntax, but folks generally\
3624+ \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3625+ ]
3626+
3627+
3628+addNoteForRecordTypeIndentError :: D.Doc -> D.Doc
3629+addNoteForRecordTypeIndentError normalRecommendation =
3630+ D.stack $
3631+ normalRecommendation
3632+ :
3633+ [ D.toSimpleNote
3634+ "I may be confused by indentation. For example, if you are trying to define\
3635+ \ a record type across multiple lines, I recommend using this format:"
3636+ , D.indent 4 $ D.vcat $
3637+ [ "{ name : String"
3638+ , ", age : Int"
3639+ , ", height : Float"
3640+ , "}"
3641+ ]
3642+ , D.reflow $
3643+ "Notice that each line starts with some indentation. Usually two or four spaces. I\
3644+ \ know this style can be jarring for people coming from C-like syntax, but folks generally\
3645+ \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3646+ ]
3434
3647
3435
3648
3436toTTupleReport :: Code.Source -> TTuple -> Row -> Col -> Report.Report
3649toTTupleReport :: Code.Source -> TTuple -> Row -> Col -> Report.Report