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

fix(compiler): improve error diagnostic for invalid string concat #7025

Merged
merged 4 commits into from
Aug 19, 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
8 changes: 8 additions & 0 deletions examples/tests/invalid/stringify.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ log(b);

log(foo);
// ^ Expected type to be "stringable", but got "Foo" instead

let z = 42;

log("value: " + z);
// ^ Binary operator '+' cannot be applied to operands of type 'str' and 'num'

log(z + " is the value");
// ^ Binary operator '+' cannot be applied to operands of type 'str' and 'num'
25 changes: 16 additions & 9 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2296,17 +2296,24 @@ It should primarily be used in preflight or in inflights that are guaranteed to
} else {
// If any of the types are unresolved (error) then don't report this assuming the error has already been reported
if !ltype.is_unresolved() && !rtype.is_unresolved() {
self.spanned_error(
let mut hints = vec![];
// If one of the operands is a string type, add a hint to use string interpolation
if ltype.is_subtype_of(&self.types.string()) || rtype.is_subtype_of(&self.types.string()) {
hints.push("Consider using string interpolation: \"Hello, {name}\"".to_string());
}

self.spanned_error_with_hints(
exp,
format!(
"Binary operator '+' cannot be applied to operands of type '{}' and '{}'; only ({}, {}) and ({}, {}) are supported",
ltype,
rtype,
self.types.number(),
self.types.number(),
self.types.string(),
self.types.string(),
),
"Binary operator '+' cannot be applied to operands of type '{}' and '{}'; only ({}, {}) and ({}, {}) are supported",
ltype,
rtype,
self.types.number(),
self.types.number(),
self.types.string(),
self.types.string(),
),
&hints,
);
}
self.resolved_error()
Expand Down
32 changes: 32 additions & 0 deletions tools/hangar/__snapshots__/invalid.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ error: Binary operator '+' cannot be applied to operands of type 'num' and 'str'
|
185 | 2 + "2";
| ^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"


error: Expected type to be "num", but got "str" instead
Expand All @@ -1002,6 +1004,8 @@ error: Binary operator '+' cannot be applied to operands of type 'num' and 'str'
|
198 | 2 + "2";
| ^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"


error: Expected type to be "str", but got "num" instead
Expand All @@ -1016,6 +1020,8 @@ error: Binary operator '+' cannot be applied to operands of type 'num' and 'str'
|
210 | 2 + "2";
| ^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"


error: Expected type to be "num", but got "str" instead
Expand All @@ -1030,6 +1036,8 @@ error: Binary operator '+' cannot be applied to operands of type 'num' and 'str'
|
221 | 2 + "2";
| ^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"


error: Expected type to be "str", but got "num" instead
Expand Down Expand Up @@ -2307,6 +2315,8 @@ error: Binary operator '+' cannot be applied to operands of type 'num' and 'str'
|
36 | let var x = 3 + "hello";
| ^^^^^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"


error: Unsupported reassignment of element of type unresolved
Expand Down Expand Up @@ -4629,6 +4639,24 @@ error: Expected type to be "stringable", but got "Foo" instead
|
= hint: str, num, bool, json, and enums are stringable


error: Binary operator '+' cannot be applied to operands of type 'str' and 'num'; only (num, num) and (str, str) are supported
--> ../../../examples/tests/invalid/stringify.test.w:25:5
|
25 | log("value: " + z);
| ^^^^^^^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"


error: Binary operator '+' cannot be applied to operands of type 'num' and 'str'; only (num, num) and (str, str) are supported
--> ../../../examples/tests/invalid/stringify.test.w:28:5
|
28 | log(z + " is the value");
| ^^^^^^^^^^^^^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"

Tests 1 failed (1)
Snapshots 1 skipped
Test Files 1 failed (1)
Expand Down Expand Up @@ -4974,6 +5002,8 @@ exports[`types_strings_arithmetic.test.w 1`] = `
|
1 | let e1 = 2 + "2";
| ^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"


error: Expected type to be "num", but got "str" instead
Expand Down Expand Up @@ -5188,6 +5218,8 @@ error: Binary operator '+' cannot be applied to operands of type 'num' and 'str'
|
20 | this.bucket.assert(2 + "2");
| ^^^^^^^
|
= hint: Consider using string interpolation: "Hello, {name}"


error: Member "assert" does not exist in "Bucket"
Expand Down
Loading