Skip to content

Commit

Permalink
fix(compiler): improve error diagnostic for invalid string concat (#7025
Browse files Browse the repository at this point in the history
)

Co-authored-by: wingbot <[email protected]>
Co-authored-by: monada-bot[bot] <[email protected]>
  • Loading branch information
3 people authored Aug 19, 2024
1 parent 1dc08f5 commit 75ca07a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
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

0 comments on commit 75ca07a

Please sign in to comment.