Skip to content

Commit 1d5cfb3

Browse files
giacomocavalierilpil
authored andcommitted
improve deprecation error message
1 parent 61c5076 commit 1d5cfb3

7 files changed

Lines changed: 102 additions & 5 deletions

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@
2323
update syntax with variants that have no labelled fields.
2424
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
2525

26+
- The error message for invalid deprecated attributes with no deprecation
27+
message has been improved. For example, the following code:
28+
29+
```gleam
30+
pub type HashAlgorithm {
31+
@deprecated
32+
Md5
33+
Sha224
34+
Sha512
35+
}
36+
```
37+
38+
Will raise the following error:
39+
40+
```txt
41+
error: Syntax error
42+
┌─ /src/parse/error.gleam:3:3
43+
44+
3 │ @deprecated
45+
│ ^^^^^^^^^^^ A deprecation attribute must have a string message.
46+
47+
See: https://tour.gleam.run/functions/deprecations/
48+
```
49+
50+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
51+
2652
- The compiler now raises a warning on the JavaScript target when defining an
2753
integer segment with a size higher than 52 bits. For example, this code:
2854

compiler-core/src/parse.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4488,10 +4488,7 @@ functions are declared separately from types.";
44884488
self.parse_external_attribute(start, end, attributes)
44894489
}
44904490
"target" => self.parse_target_attribute(start, end, attributes),
4491-
"deprecated" => {
4492-
let _ = self.expect_one(&Token::LeftParen)?;
4493-
self.parse_deprecated_attribute(start, end, attributes)
4494-
}
4491+
"deprecated" => self.parse_deprecated_attribute(start, end, attributes),
44954492
"internal" => self.parse_internal_attribute(start, end, attributes),
44964493
_ => parse_error(ParseErrorType::UnknownAttribute, SrcSpan { start, end }),
44974494
}?;
@@ -4553,6 +4550,10 @@ functions are declared separately from types.";
45534550
end: u32,
45544551
attributes: &mut Attributes,
45554552
) -> Result<u32, ParseError> {
4553+
let _ = self.expect_one(&Token::LeftParen).map_err(|_| ParseError {
4554+
error: ParseErrorType::ExpectedDeprecationMessage,
4555+
location: SrcSpan { start, end },
4556+
})?;
45564557
if attributes.deprecated.is_deprecated() {
45574558
return parse_error(ParseErrorType::DuplicateAttribute, SrcSpan::new(start, end));
45584559
}

compiler-core/src/parse/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl ParseErrorType {
216216
},
217217

218218
ParseErrorType::ExpectedDeprecationMessage => ParseErrorDetails {
219-
text: "".into(),
219+
text: "See: https://tour.gleam.run/functions/deprecations/".into(),
220220
hint: None,
221221
label_text: "A deprecation attribute must have a string message.".into(),
222222
extra_labels: vec![],
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: compiler-core/src/parse/tests.rs
3+
expression: "\n@deprecated\npub fn main() -> Nil {\n Nil\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
@deprecated
8+
pub fn main() -> Nil {
9+
Nil
10+
}
11+
12+
13+
----- ERROR
14+
error: Syntax error
15+
┌─ /src/parse/error.gleam:2:1
16+
17+
2 │ @deprecated
18+
^^^^^^^^^^^ A deprecation attribute must have a string message.
19+
20+
See: https://tour.gleam.run/functions/deprecations/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: compiler-core/src/parse/tests.rs
3+
expression: "\npub type HashAlgorithm {\n @deprecated\n Md5\n Sha224\n Sha512\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub type HashAlgorithm {
8+
@deprecated
9+
Md5
10+
Sha224
11+
Sha512
12+
}
13+
14+
15+
----- ERROR
16+
error: Syntax error
17+
┌─ /src/parse/error.gleam:3:3
18+
19+
3 │ @deprecated
20+
│ ^^^^^^^^^^^ A deprecation attribute must have a string message.
21+
22+
See: https://tour.gleam.run/functions/deprecations/

compiler-core/src/parse/snapshots/gleam_core__parse__tests__deprecation_without_message.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ error: Syntax error
1616
1717
2 │ @deprecated()
1818
^^^^^^^^^^^ A deprecation attribute must have a string message.
19+
20+
See: https://tour.gleam.run/functions/deprecations/

compiler-core/src/parse/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,32 @@ pub fn main() -> Nil {
770770
);
771771
}
772772

773+
#[test]
774+
fn deprecation_with_no_message() {
775+
assert_module_error!(
776+
r#"
777+
@deprecated
778+
pub fn main() -> Nil {
779+
Nil
780+
}
781+
"#
782+
);
783+
}
784+
785+
#[test]
786+
fn deprecation_with_no_message_on_constructor() {
787+
assert_module_error!(
788+
r#"
789+
pub type HashAlgorithm {
790+
@deprecated
791+
Md5
792+
Sha224
793+
Sha512
794+
}
795+
"#
796+
);
797+
}
798+
773799
#[test]
774800
fn deprecation_without_message() {
775801
assert_module_error!(

0 commit comments

Comments
 (0)