Skip to content

Commit ae459f8

Browse files
AaronC81lpil
authored andcommitted
Add specific parser error for angle-bracket generics in function definitions
1 parent 5d0437b commit ae459f8

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

compiler-core/src/parse.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,15 @@ where
20812081
n,
20822082
));
20832083
}
2084+
if let Some((less_start, less_end)) = self.maybe_one(&Token::Less) {
2085+
return Err(ParseError {
2086+
error: ParseErrorType::FunctionDefinitionAngleGenerics,
2087+
location: SrcSpan {
2088+
start: less_start,
2089+
end: less_end,
2090+
},
2091+
});
2092+
}
20842093
let _ = self
20852094
.expect_one(&Token::LeftParen)
20862095
.map_err(|e| self.add_anon_function_hint(e))?;

compiler-core/src/parse/error.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub enum ParseErrorType {
117117
EmptyGuardBlock,
118118
// When the use tries to define a constant inside a function
119119
ConstantInsideFunction,
120+
FunctionDefinitionAngleGenerics, // fn something<T>() { ... }
120121
}
121122

122123
pub(crate) struct ParseErrorDetails {
@@ -651,6 +652,21 @@ functions are not necessary.",
651652
label_text: "Constants are not allowed inside functions".into(),
652653
extra_labels: vec![],
653654
},
655+
656+
ParseErrorType::FunctionDefinitionAngleGenerics => ParseErrorDetails {
657+
text: "\
658+
Generic function type variables do not need to be predeclared like they
659+
would be in some other languages, instead they are written with lowercase
660+
names.
661+
662+
fn example(argument: generic) -> generic
663+
664+
See: https://tour.gleam.run/functions/generic-functions/"
665+
.into(),
666+
hint: None,
667+
label_text: "I was expecting `(` here.".into(),
668+
extra_labels: vec![],
669+
},
654670
}
655671
}
656672
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: compiler-core/src/parse/tests.rs
3+
expression: "fn id<T>(x: T) { x }"
4+
---
5+
----- SOURCE CODE
6+
fn id<T>(x: T) { x }
7+
8+
----- ERROR
9+
error: Syntax error
10+
┌─ /src/parse/error.gleam:1:6
11+
12+
1fn id<T>(x: T) { x }
13+
^ I was expecting `(` here.
14+
15+
Generic function type variables do not need to be predeclared like they
16+
would be in some other languages, instead they are written with lowercase
17+
names.
18+
19+
fn example(argument: generic) -> generic
20+
21+
See: https://tour.gleam.run/functions/generic-functions/

compiler-core/src/parse/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,3 +1942,8 @@ pub fn main() {
19421942
"
19431943
);
19441944
}
1945+
1946+
#[test]
1947+
fn function_definition_angle_generics_error() {
1948+
assert_module_error!("fn id<T>(x: T) { x }");
1949+
}

0 commit comments

Comments
 (0)