mirror of
				https://github.com/roc-lang/roc.git
				synced 2025-11-03 22:13:35 +00:00 
			
		
		
		
	ignore final suffix for annotated top-level defs
This commit is contained in:
		
							parent
							
								
									f1f7dddab7
								
							
						
					
					
						commit
						afb247d4c3
					
				
					 4 changed files with 109 additions and 3 deletions
				
			
		| 
						 | 
				
			
			@ -248,8 +248,7 @@ pub fn desugar_value_def_suffixed<'a>(arena: &'a Bump, value_def: ValueDef<'a>)
 | 
			
		|||
            body_pattern,
 | 
			
		||||
            body_expr,
 | 
			
		||||
        } => {
 | 
			
		||||
            // note called_from_def is passed as `false` as this is a top_level_def
 | 
			
		||||
            match unwrap_suffixed_expression(arena, body_expr, None) {
 | 
			
		||||
            match unwrap_suffixed_expression(arena, body_expr, Some(ann_pattern)) {
 | 
			
		||||
                Ok(new_expr) => AnnotatedBody {
 | 
			
		||||
                    ann_pattern,
 | 
			
		||||
                    ann_type,
 | 
			
		||||
| 
						 | 
				
			
			@ -280,6 +279,19 @@ pub fn desugar_value_def_suffixed<'a>(arena: &'a Bump, value_def: ValueDef<'a>)
 | 
			
		|||
                        ),
 | 
			
		||||
                    },
 | 
			
		||||
                ),
 | 
			
		||||
                // When the last expression is suffixed, it will try to unwrap the def, but because we
 | 
			
		||||
                // have an annotated def we can simply ignore the try and return it as is without
 | 
			
		||||
                // creating an intermediate identifier
 | 
			
		||||
                Err(EUnwrapped::UnwrappedDefExpr { loc_expr, .. }) => desugar_value_def_suffixed(
 | 
			
		||||
                    arena,
 | 
			
		||||
                    AnnotatedBody {
 | 
			
		||||
                        ann_pattern,
 | 
			
		||||
                        ann_type,
 | 
			
		||||
                        lines_between,
 | 
			
		||||
                        body_pattern,
 | 
			
		||||
                        body_expr: loc_expr,
 | 
			
		||||
                    },
 | 
			
		||||
                ),
 | 
			
		||||
                Err(..) => AnnotatedBody {
 | 
			
		||||
                    ann_pattern,
 | 
			
		||||
                    ann_type,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -325,7 +325,14 @@ pub fn unwrap_suffixed_expression_apply_help<'a>(
 | 
			
		|||
 | 
			
		||||
                let new_apply = arena.alloc(Loc::at(loc_expr.region, Expr::Apply(unwrapped_function, local_args, called_via)));
 | 
			
		||||
 | 
			
		||||
                return init_unwrapped_err(arena, new_apply, maybe_def_pat, target);
 | 
			
		||||
                match maybe_def_pat {
 | 
			
		||||
                    Some(..) => {
 | 
			
		||||
                        return Err(EUnwrapped::UnwrappedDefExpr { loc_expr: new_apply, target });
 | 
			
		||||
                    }
 | 
			
		||||
                    None => {
 | 
			
		||||
                        return init_unwrapped_err(arena, new_apply, maybe_def_pat, target);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // function is another expression
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,75 @@
 | 
			
		|||
---
 | 
			
		||||
source: crates/compiler/can/tests/test_suffixed.rs
 | 
			
		||||
expression: snapshot
 | 
			
		||||
---
 | 
			
		||||
Defs {
 | 
			
		||||
    tags: [
 | 
			
		||||
        Index(2147483648),
 | 
			
		||||
        Index(2147483649),
 | 
			
		||||
    ],
 | 
			
		||||
    regions: [
 | 
			
		||||
        @0-33,
 | 
			
		||||
        @35-45,
 | 
			
		||||
    ],
 | 
			
		||||
    space_before: [
 | 
			
		||||
        Slice(start = 0, length = 0),
 | 
			
		||||
        Slice(start = 0, length = 2),
 | 
			
		||||
    ],
 | 
			
		||||
    space_after: [
 | 
			
		||||
        Slice(start = 0, length = 0),
 | 
			
		||||
        Slice(start = 2, length = 1),
 | 
			
		||||
    ],
 | 
			
		||||
    spaces: [
 | 
			
		||||
        Newline,
 | 
			
		||||
        Newline,
 | 
			
		||||
        Newline,
 | 
			
		||||
    ],
 | 
			
		||||
    type_defs: [],
 | 
			
		||||
    value_defs: [
 | 
			
		||||
        AnnotatedBody {
 | 
			
		||||
            ann_pattern: @0-3 Identifier {
 | 
			
		||||
                ident: "run",
 | 
			
		||||
            },
 | 
			
		||||
            ann_type: @6-15 Apply(
 | 
			
		||||
                "",
 | 
			
		||||
                "Task",
 | 
			
		||||
                [
 | 
			
		||||
                    @11-13 Record {
 | 
			
		||||
                        fields: [],
 | 
			
		||||
                        ext: None,
 | 
			
		||||
                    },
 | 
			
		||||
                    @14-15 Inferred,
 | 
			
		||||
                ],
 | 
			
		||||
            ),
 | 
			
		||||
            lines_between: [
 | 
			
		||||
                Newline,
 | 
			
		||||
            ],
 | 
			
		||||
            body_pattern: @16-19 Identifier {
 | 
			
		||||
                ident: "run",
 | 
			
		||||
            },
 | 
			
		||||
            body_expr: @22-33 Apply(
 | 
			
		||||
                @22-33 Var {
 | 
			
		||||
                    module_name: "",
 | 
			
		||||
                    ident: "line",
 | 
			
		||||
                },
 | 
			
		||||
                [
 | 
			
		||||
                    @28-33 Str(
 | 
			
		||||
                        PlainLine(
 | 
			
		||||
                            "foo",
 | 
			
		||||
                        ),
 | 
			
		||||
                    ),
 | 
			
		||||
                ],
 | 
			
		||||
                Space,
 | 
			
		||||
            ),
 | 
			
		||||
        },
 | 
			
		||||
        Body(
 | 
			
		||||
            @35-39 Identifier {
 | 
			
		||||
                ident: "main",
 | 
			
		||||
            },
 | 
			
		||||
            @42-45 Var {
 | 
			
		||||
                module_name: "",
 | 
			
		||||
                ident: "run",
 | 
			
		||||
            },
 | 
			
		||||
        ),
 | 
			
		||||
    ],
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -632,6 +632,18 @@ mod suffixed_tests {
 | 
			
		|||
            "##
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn issue_7103() {
 | 
			
		||||
        run_test!(
 | 
			
		||||
            r##"
 | 
			
		||||
            run : Task {} _
 | 
			
		||||
            run = line! "foo"
 | 
			
		||||
 | 
			
		||||
            main = run
 | 
			
		||||
            "##
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue