From 47e490407553acd51f7676fb90eaff0f440bc35b Mon Sep 17 00:00:00 2001 From: ayazhafiz Date: Sun, 6 Mar 2022 22:15:02 -0500 Subject: [PATCH] Chase aliases when checking for valid extension types --- compiler/can/src/annotation.rs | 2 +- compiler/solve/tests/solve_expr.rs | 30 ++++++++++++++++++++++++++++++ compiler/types/src/types.rs | 7 ++++--- reporting/tests/test_reporting.rs | 4 ++-- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/compiler/can/src/annotation.rs b/compiler/can/src/annotation.rs index 1fbe8a3652..9ace3a1a7d 100644 --- a/compiler/can/src/annotation.rs +++ b/compiler/can/src/annotation.rs @@ -682,7 +682,7 @@ fn can_extension_type<'a>( local_aliases, references, ); - if valid_extension_type(&ext_type) { + if valid_extension_type(&ext_type.shallow_dealias()) { ext_type } else { // Report an error but mark the extension variable to be inferred diff --git a/compiler/solve/tests/solve_expr.rs b/compiler/solve/tests/solve_expr.rs index 70b321afcd..4c4f36de14 100644 --- a/compiler/solve/tests/solve_expr.rs +++ b/compiler/solve/tests/solve_expr.rs @@ -5543,4 +5543,34 @@ mod solve_expr { "Str", ) } + + #[test] + fn record_extension_variable_is_alias() { + infer_eq_without_problem( + indoc!( + r#" + Other a b : { y: a, z: b } + + f : { x : Str }(Other Str Str) + f + "# + ), + r#"{ x : Str, y : Str, z : Str }"#, + ) + } + + #[test] + fn tag_extension_variable_is_alias() { + infer_eq_without_problem( + indoc!( + r#" + Other : [ B, C ] + + f : [ A ]Other + f + "# + ), + r#"[ A, B, C ]"#, + ) + } } diff --git a/compiler/types/src/types.rs b/compiler/types/src/types.rs index 6bc9b7ed9e..c6f4f360f3 100644 --- a/compiler/types/src/types.rs +++ b/compiler/types/src/types.rs @@ -722,10 +722,11 @@ impl Type { /// a shallow dealias, continue until the first constructor is not an alias. pub fn shallow_dealias(&self) -> &Self { - match self { - Type::Alias { actual, .. } => actual.shallow_dealias(), - _ => self, + let mut result = self; + while let Type::Alias { actual, .. } = result { + result = actual; } + result } pub fn instantiate_aliases( diff --git a/reporting/tests/test_reporting.rs b/reporting/tests/test_reporting.rs index 4f1fc68969..247db7a0ec 100644 --- a/reporting/tests/test_reporting.rs +++ b/reporting/tests/test_reporting.rs @@ -8546,7 +8546,7 @@ I need all branches in an `if` to have the same type! report_problem_as( indoc!( r#" - f : [ A ]U32 + f : [ A ]Str f "# ), @@ -8556,7 +8556,7 @@ I need all branches in an `if` to have the same type! This tag union extension type is invalid: - 1│ f : [ A ]U32 + 1│ f : [ A ]Str ^^^ Note: A tag union extension variable can only contain a type variable