Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve clarity of error messages for unknown record fields. #3964

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions compiler-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,8 @@ Hint: Add some type annotations and try again.")
if variants == &UnknownField::NoFields {
text.push_str("\nIt does not have any fields.");
} else {
text.push_str("\nIt does not have any fields shared by all variants.");
text.push_str("\nIt does not have fields that are common \
across all variants.");
}
} else {
text.push_str("\nIt has these accessible fields:\n");
Expand All @@ -1712,21 +1713,19 @@ Hint: Add some type annotations and try again.")
match variants {
UnknownField::AppearsInAVariant => {
let msg = wrap(
"Note: The field you are trying to \
access might not be consistently present or positioned across the custom \
type's variants, preventing reliable access. Ensure the field exists in the \
same position and has the same type in all variants, or pattern matching on it \
to enable direct accessor syntax.",
"Note: The field you are trying to access is \
not defined consistently across all variants of this custom type. To fix this, \
ensure that all variants include the field with the same name, position, and \
type.",
);
text.push_str("\n\n");
text.push_str(&msg);
}
UnknownField::AppearsInAnImpossibleVariant => {
let msg = wrap(
"Note: The field you are trying to \
access exists, but not on the variant which this value always is. \
A field that is not present in all variants can only be accessed when \
the value is inferred to be one variant.",
"Note: The field exists in this custom type \
but is not defined for the current variant. Ensure that you are accessing the \
field on a variant where it is valid.",
);
text.push_str("\n\n");
text.push_str(&msg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: compiler-core/src/type_/tests.rs
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"
snapshot_kind: text
---
----- SOURCE CODE

Expand Down Expand Up @@ -35,8 +34,6 @@ It has these accessible fields:

.a

Note: The field you are trying to access might not be consistently present
or positioned across the custom type's variants, preventing reliable
access. Ensure the field exists in the same position and has the same type
in all variants, or pattern matching on it to enable direct accessor
syntax.
Note: The field you are trying to access is not defined consistently across
all variants of this custom type. To fix this, ensure that all variants
include the field with the same name, position, and type.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: compiler-core/src/type_/tests.rs
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"
snapshot_kind: text
---
----- SOURCE CODE

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

Thingy

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

Note: The field you are trying to access might not be consistently present
or positioned across the custom type's variants, preventing reliable
access. Ensure the field exists in the same position and has the same type
in all variants, or pattern matching on it to enable direct accessor
syntax.
Note: The field you are trying to access is not defined consistently across
all variants of this custom type. To fix this, ensure that all variants
include the field with the same name, position, and type.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: compiler-core/src/type_/tests/errors.rs
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 }"
snapshot_kind: text
---
----- SOURCE CODE

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

.name

Note: The field you are trying to access might not be consistently present
or positioned across the custom type's variants, preventing reliable
access. Ensure the field exists in the same position and has the same type
in all variants, or pattern matching on it to enable direct accessor
syntax.
Note: The field you are trying to access is not defined consistently across
all variants of this custom type. To fix this, ensure that all variants
include the field with the same name, position, and type.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: compiler-core/src/type_/tests/errors.rs
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 }"
snapshot_kind: text
---
----- SOURCE CODE

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

.age

Note: The field you are trying to access might not be consistently present
or positioned across the custom type's variants, preventing reliable
access. Ensure the field exists in the same position and has the same type
in all variants, or pattern matching on it to enable direct accessor
syntax.
Note: The field you are trying to access is not defined consistently across
all variants of this custom type. To fix this, ensure that all variants
include the field with the same name, position, and type.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: compiler-core/src/type_/tests/errors.rs
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 }"
snapshot_kind: text
---
----- SOURCE CODE

Expand All @@ -27,8 +26,6 @@ It has these accessible fields:
.age
.name

Note: The field you are trying to access might not be consistently present
or positioned across the custom type's variants, preventing reliable
access. Ensure the field exists in the same position and has the same type
in all variants, or pattern matching on it to enable direct accessor
syntax.
Note: The field you are trying to access is not defined consistently across
all variants of this custom type. To fix this, ensure that all variants
include the field with the same name, position, and type.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: compiler-core/src/type_/tests/errors.rs
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"
snapshot_kind: text
---
----- SOURCE CODE

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

Shape

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

Note: The field you are trying to access might not be consistently present
or positioned across the custom type's variants, preventing reliable
access. Ensure the field exists in the same position and has the same type
in all variants, or pattern matching on it to enable direct accessor
syntax.
Note: The field you are trying to access is not defined consistently across
all variants of this custom type. To fix this, ensure that all variants
include the field with the same name, position, and type.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ It has these accessible fields:

.wibble

Note: The field you are trying to access exists, but not on the variant
which this value always is. A field that is not present in all variants can
only be accessed when the value is inferred to be one variant.
Note: The field exists in this custom type but is not defined for the
current variant. Ensure that you are accessing the field on a variant where
it is valid.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: compiler-core/src/type_/tests/errors.rs
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"
snapshot_kind: text
---
----- SOURCE CODE

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

Wibble

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

Note: The field you are trying to access might not be consistently present
or positioned across the custom type's variants, preventing reliable
access. Ensure the field exists in the same position and has the same type
in all variants, or pattern matching on it to enable direct accessor
syntax.
Note: The field you are trying to access is not defined consistently across
all variants of this custom type. To fix this, ensure that all variants
include the field with the same name, position, and type.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\nimport some_mod\npub fn main(wibble: some_mod.Wibble) {\n wibble.field\n}\n"
snapshot_kind: text
---
----- SOURCE CODE
-- some_mod.gleam
Expand Down Expand Up @@ -29,10 +28,8 @@ The value being accessed has this type:

some_mod.Wibble

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

Note: The field you are trying to access might not be consistently present
or positioned across the custom type's variants, preventing reliable
access. Ensure the field exists in the same position and has the same type
in all variants, or pattern matching on it to enable direct accessor
syntax.
Note: The field you are trying to access is not defined consistently across
all variants of this custom type. To fix this, ensure that all variants
include the field with the same name, position, and type.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ The value being accessed has this type:

Wibble

It does not have any fields shared by all variants.
It does not have fields that are common across all variants.
Loading