Skip to content

Commit b96a39c

Browse files
tubedudelpil
authored andcommitted
Improve clarity of error messages for unknown record fields.
1 parent a965f8e commit b96a39c

11 files changed

+41
-66
lines changed

compiler-core/src/error.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,8 @@ Hint: Add some type annotations and try again.")
16991699
if variants == &UnknownField::NoFields {
17001700
text.push_str("\nIt does not have any fields.");
17011701
} else {
1702-
text.push_str("\nIt does not have any fields shared by all variants.");
1702+
text.push_str("\nIt does not have fields that are common \
1703+
across all variants.");
17031704
}
17041705
} else {
17051706
text.push_str("\nIt has these accessible fields:\n");
@@ -1712,21 +1713,19 @@ Hint: Add some type annotations and try again.")
17121713
match variants {
17131714
UnknownField::AppearsInAVariant => {
17141715
let msg = wrap(
1715-
"Note: The field you are trying to \
1716-
access might not be consistently present or positioned across the custom \
1717-
type's variants, preventing reliable access. Ensure the field exists in the \
1718-
same position and has the same type in all variants, or pattern matching on it \
1719-
to enable direct accessor syntax.",
1716+
"Note: The field you are trying to access is \
1717+
not defined consistently across all variants of this custom type. To fix this, \
1718+
ensure that all variants include the field with the same name, position, and \
1719+
type.",
17201720
);
17211721
text.push_str("\n\n");
17221722
text.push_str(&msg);
17231723
}
17241724
UnknownField::AppearsInAnImpossibleVariant => {
17251725
let msg = wrap(
1726-
"Note: The field you are trying to \
1727-
access exists, but not on the variant which this value always is. \
1728-
A field that is not present in all variants can only be accessed when \
1729-
the value is inferred to be one variant.",
1726+
"Note: The field exists in this custom type \
1727+
but is not defined for the current variant. Ensure that you are accessing the \
1728+
field on a variant where it is valid.",
17301729
);
17311730
text.push_str("\n\n");
17321731
text.push_str(&msg);

compiler-core/src/type_/snapshots/gleam_core__type___tests__type_unification_does_not_allow_different_variants_to_be_treated_as_safe.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: compiler-core/src/type_/tests.rs
33
expression: "\npub type Wibble {\n Wibble(a: Int, b: Int)\n Wobble(a: Int, c: String)\n}\n\npub fn main() {\n let a = case todo {\n Wibble(..) as b -> Wibble(..b, b: 1)\n Wobble(..) as b -> Wobble(..b, c: \"a\")\n }\n\n a.b\n}\n"
4-
snapshot_kind: text
54
---
65
----- SOURCE CODE
76

@@ -35,8 +34,6 @@ It has these accessible fields:
3534

3635
.a
3736

38-
Note: The field you are trying to access might not be consistently present
39-
or positioned across the custom type's variants, preventing reliable
40-
access. Ensure the field exists in the same position and has the same type
41-
in all variants, or pattern matching on it to enable direct accessor
42-
syntax.
37+
Note: The field you are trying to access is not defined consistently across
38+
all variants of this custom type. To fix this, ensure that all variants
39+
include the field with the same name, position, and type.

compiler-core/src/type_/snapshots/gleam_core__type___tests__variant_inference_does_not_escape_clause_scope.snap

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: compiler-core/src/type_/tests.rs
33
expression: "\npub type Thingy {\n A(a: Int)\n B(x: Int, b: Int)\n}\n\npub fn fun(x) {\n case x {\n A(..) -> x.a\n B(..) -> x.b\n }\n x.b\n}\n"
4-
snapshot_kind: text
54
---
65
----- SOURCE CODE
76

@@ -30,10 +29,8 @@ The value being accessed has this type:
3029

3130
Thingy
3231

33-
It does not have any fields shared by all variants.
32+
It does not have fields that are common across all variants.
3433

35-
Note: The field you are trying to access might not be consistently present
36-
or positioned across the custom type's variants, preventing reliable
37-
access. Ensure the field exists in the same position and has the same type
38-
in all variants, or pattern matching on it to enable direct accessor
39-
syntax.
34+
Note: The field you are trying to access is not defined consistently across
35+
all variants of this custom type. To fix this, ensure that all variants
36+
include the field with the same name, position, and type.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__accessor_multiple_variants_multiple_positions.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: compiler-core/src/type_/tests/errors.rs
33
expression: "\npub type Person {\n Teacher(name: String, title: String, age: Int)\n Student(name: String, age: Int)\n}\npub fn get_name(person: Person) { person.name }\npub fn get_age(person: Person) { person.age }"
4-
snapshot_kind: text
54
---
65
----- SOURCE CODE
76

@@ -27,8 +26,6 @@ It has these accessible fields:
2726

2827
.name
2928

30-
Note: The field you are trying to access might not be consistently present
31-
or positioned across the custom type's variants, preventing reliable
32-
access. Ensure the field exists in the same position and has the same type
33-
in all variants, or pattern matching on it to enable direct accessor
34-
syntax.
29+
Note: The field you are trying to access is not defined consistently across
30+
all variants of this custom type. To fix this, ensure that all variants
31+
include the field with the same name, position, and type.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__accessor_multiple_variants_multiple_positions2.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: compiler-core/src/type_/tests/errors.rs
33
expression: "\npub type Person {\n Teacher(title: String, age: Int, name: String)\n Student(name: String, age: Int)\n}\npub fn get_name(person: Person) { person.name }\npub fn get_age(person: Person) { person.age }"
4-
snapshot_kind: text
54
---
65
----- SOURCE CODE
76

@@ -27,8 +26,6 @@ It has these accessible fields:
2726

2827
.age
2928

30-
Note: The field you are trying to access might not be consistently present
31-
or positioned across the custom type's variants, preventing reliable
32-
access. Ensure the field exists in the same position and has the same type
33-
in all variants, or pattern matching on it to enable direct accessor
34-
syntax.
29+
Note: The field you are trying to access is not defined consistently across
30+
all variants of this custom type. To fix this, ensure that all variants
31+
include the field with the same name, position, and type.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__field_not_in_all_variants.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: compiler-core/src/type_/tests/errors.rs
33
expression: "\npub type Person {\n Teacher(name: String, age: Int, title: String)\n Student(name: String, age: Int)\n}\npub fn get_title(person: Person) { person.title }"
4-
snapshot_kind: text
54
---
65
----- SOURCE CODE
76

@@ -27,8 +26,6 @@ It has these accessible fields:
2726
.age
2827
.name
2928

30-
Note: The field you are trying to access might not be consistently present
31-
or positioned across the custom type's variants, preventing reliable
32-
access. Ensure the field exists in the same position and has the same type
33-
in all variants, or pattern matching on it to enable direct accessor
34-
syntax.
29+
Note: The field you are trying to access is not defined consistently across
30+
all variants of this custom type. To fix this, ensure that all variants
31+
include the field with the same name, position, and type.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__field_type_different_between_variants.snap

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: compiler-core/src/type_/tests/errors.rs
33
expression: "\npub type Shape {\n Square(x: Int, y: Int)\n Rectangle(x: String, y: String)\n}\npub fn get_x(shape: Shape) { shape.x }\n"
4-
snapshot_kind: text
54
---
65
----- SOURCE CODE
76

@@ -23,10 +22,8 @@ The value being accessed has this type:
2322

2423
Shape
2524

26-
It does not have any fields shared by all variants.
25+
It does not have fields that are common across all variants.
2726

28-
Note: The field you are trying to access might not be consistently present
29-
or positioned across the custom type's variants, preventing reliable
30-
access. Ensure the field exists in the same position and has the same type
31-
in all variants, or pattern matching on it to enable direct accessor
32-
syntax.
27+
Note: The field you are trying to access is not defined consistently across
28+
all variants of this custom type. To fix this, ensure that all variants
29+
include the field with the same name, position, and type.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__record_access_on_inferred_variant_when_field_is_in_other_variants.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ It has these accessible fields:
3030

3131
.wibble
3232

33-
Note: The field you are trying to access exists, but not on the variant
34-
which this value always is. A field that is not present in all variants can
35-
only be accessed when the value is inferred to be one variant.
33+
Note: The field exists in this custom type but is not defined for the
34+
current variant. Ensure that you are accessing the field on a variant where
35+
it is valid.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__unknown_field_that_appears_in_a_variant_has_note.snap

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: compiler-core/src/type_/tests/errors.rs
33
expression: "\npub type Wibble {\n Wibble(field: Int)\n Wobble(not_field: String, field: Int)\n}\n\npub fn main(wibble: Wibble) {\n wibble.field\n}\n"
4-
snapshot_kind: text
54
---
65
----- SOURCE CODE
76

@@ -26,10 +25,8 @@ The value being accessed has this type:
2625

2726
Wibble
2827

29-
It does not have any fields shared by all variants.
28+
It does not have fields that are common across all variants.
3029

31-
Note: The field you are trying to access might not be consistently present
32-
or positioned across the custom type's variants, preventing reliable
33-
access. Ensure the field exists in the same position and has the same type
34-
in all variants, or pattern matching on it to enable direct accessor
35-
syntax.
30+
Note: The field you are trying to access is not defined consistently across
31+
all variants of this custom type. To fix this, ensure that all variants
32+
include the field with the same name, position, and type.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__unknown_field_that_appears_in_an_imported_variant_has_note.snap

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: compiler-core/src/type_/tests/errors.rs
33
expression: "\nimport some_mod\npub fn main(wibble: some_mod.Wibble) {\n wibble.field\n}\n"
4-
snapshot_kind: text
54
---
65
----- SOURCE CODE
76
-- some_mod.gleam
@@ -29,10 +28,8 @@ The value being accessed has this type:
2928

3029
some_mod.Wibble
3130

32-
It does not have any fields shared by all variants.
31+
It does not have fields that are common across all variants.
3332

34-
Note: The field you are trying to access might not be consistently present
35-
or positioned across the custom type's variants, preventing reliable
36-
access. Ensure the field exists in the same position and has the same type
37-
in all variants, or pattern matching on it to enable direct accessor
38-
syntax.
33+
Note: The field you are trying to access is not defined consistently across
34+
all variants of this custom type. To fix this, ensure that all variants
35+
include the field with the same name, position, and type.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__unknown_field_that_does_not_appear_in_variant_has_no_note.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ The value being accessed has this type:
2525

2626
Wibble
2727

28-
It does not have any fields shared by all variants.
28+
It does not have fields that are common across all variants.

0 commit comments

Comments
 (0)