Repository navigation
fix: a constructor checks its arguments against the declaration #442
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,17 @@ pub enum TypeError { | |
| #[error("Type annotation mismatch: declared {declared:?}, but value has type {actual:?}")] | ||
| AnnotationMismatch { declared: Ty, actual: Ty }, | ||
|
|
||
| /// The arguments of a constructor do not give the type that the | ||
| /// declaration of its `data` type gives. | ||
| #[error( | ||
| "constructor `{ctor}`: the declaration gives {declared:?}, but the arguments give {actual:?}" | ||
| )] | ||
| ConstructorMismatch { | ||
| ctor: String, | ||
| declared: Ty, | ||
| actual: Ty, | ||
| }, | ||
|
|
||
| #[error("Not yet supported in the core type checker: {0}. Use the surface (parse_surface_module → desugar) path instead.")] | ||
| NotYetSupportedInCore(&'static str), | ||
|
|
||
|
|
@@ -1421,17 +1432,27 @@ impl TypeChecker { | |
| let value_ty = self.check(value)?; | ||
|
|
||
| if let Some(declared) = ty_ann { | ||
| // The desugarer gives a constructor the binder `#<name>` and | ||
| // the declared type of its `data` type, which binds the type | ||
| // parameters. A written annotation binds none. | ||
| let declared = &self.instantiate(declared.clone()).0; | ||
|
Comment on lines
1434
to
+1438
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This now instantiates every annotated Useful? React with 👍 / 👎. |
||
| self.unify(s, declared, &value_ty).map_err(|error| { | ||
| if matches!(error.error, TypeError::LinearPatternDiscard(_)) { | ||
| return error; | ||
| } | ||
| self.at( | ||
| s, | ||
| TypeError::AnnotationMismatch { | ||
| let actual = self.resolve(&value_ty); | ||
| let error = match name.strip_prefix('#') { | ||
| Some(ctor) => TypeError::ConstructorMismatch { | ||
| ctor: ctor.into(), | ||
| declared: self.resolve(declared), | ||
| actual, | ||
| }, | ||
| None => TypeError::AnnotationMismatch { | ||
| declared: declared.clone(), | ||
| actual: self.resolve(&value_ty), | ||
| actual, | ||
| }, | ||
| ) | ||
| }; | ||
| self.at(s, error) | ||
| })?; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new safety check exists only in this annotated
let, butephapax_ir::expr_to_sexprserializesLetwithout itstyanddecode_exprrestores it asNone. Consequently, serializing a desugared module and compiling it through the publiccompile_sexpr_module/CLI S-expression path silently removes the check, so invalid terms such asdata Box = MkBox(I32)withMkBox(true)are accepted again. Either the IR codec must preserve this annotation or the constructor invariant must be represented by a core form that survives the round trip.Useful? React with 👍 / 👎.