Skip to content

Commit

Permalink
fix(compiler): missing type check for partially inferred function (#3719
Browse files Browse the repository at this point in the history
)

Fixes #3714

When making an inference in type validation, the inferred type was originally thrown away. This change instead unwraps it if needed since updating the original typeref itself won't happen til later. It's worth noting the error message will be missing information for deeply inferred types (like the return type).

Also it turns out this regression was noticeable in the snapshots before, but not noticed. So the snapshot update included 2 changes.

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
MarkMcCulloh authored Aug 7, 2023
1 parent dbfedd6 commit fea885a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion examples/tests/invalid/inference.w
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ class NeedAnnotations {
// ^^^^ Missing required type annotation for method signature
log(nice);
}
}
}

let badFunc: inflight (str): void = inflight (arg1: num) => {};
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected type to be "inflight (str): void", but got "inflight (arg1: num): unknown" instead
3 changes: 2 additions & 1 deletion libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,8 @@ impl<'a> TypeChecker<'a> {
if expected_types.len() == 1 {
// First check if the actual type is an inference that can be replaced with the expected type
if self.add_new_inference(&actual_type, &expected_types[0]) {
return_type = expected_types[0];
// Update the type we validate and return
return_type = self.types.maybe_unwrap_inference(return_type);
} else {
// otherwise, check if the expected type is an inference that can be replaced with the actual type
self.add_new_inference(&expected_types[0], &actual_type);
Expand Down
18 changes: 18 additions & 0 deletions tools/hangar/__snapshots__/invalid.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,17 @@ exports[`cloud_function_expects_inflight.w 1`] = `
| \\\\-^ Expected type to be \\"inflight (event: str): void\\", but got \\"preflight (name: str): str\\" instead
error: Expected type to be \\"inflight (message: str): void\\", but got \\"inflight (x: num): unknown\\" instead
--> ../../../examples/tests/invalid/cloud_function_expects_inflight.w:9:15
|
9 | q.setConsumer(inflight (x: num) => {
| /---------------^
10 | | // ^ \\"num\\" doesn't match the expected type \\"str\\"
11 | | return;
12 | | });
| \\\\-^ Expected type to be \\"inflight (message: str): void\\", but got \\"inflight (x: num): unknown\\" instead
Tests 1 failed (1)
Expand Down Expand Up @@ -882,6 +893,13 @@ error: Expected \\"Json\\" elements to be Json values (https://www.json.org/json
| ^^^^^^^^^^^^^^^^^ Expected \\"Json\\" elements to be Json values (https://www.json.org/json-en.html), but got \\"Array<unknown>\\" which is not a Json value
error: Expected type to be \\"inflight (str): void\\", but got \\"inflight (arg1: num): unknown\\" instead
--> ../../../examples/tests/invalid/inference.w:85:37
|
85 | let badFunc: inflight (str): void = inflight (arg1: num) => {};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected type to be \\"inflight (str): void\\", but got \\"inflight (arg1: num): unknown\\" instead
error: Unexpected return value from void function. Return type annotations are required for methods.
--> ../../../examples/tests/invalid/inference.w:76:5
|
Expand Down

0 comments on commit fea885a

Please sign in to comment.