Show the actual type of RHS on type error when LHS is a pattern with a type annotation#19284
Conversation
❗ Release notes requiredCaution No release notes found for the changed paths (see table below). Please make sure to add an entry with an informative description of the change as well as link to this pull request, issue and language suggestion if applicable. Release notes for this repository are based on Keep A Changelog format. The following format is recommended for this repository:
If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request. You can open this PR in browser to add release notes: open in github.dev
|
| | Some (SynBindingReturnInfo(typeName = retInfoTy; range = mRetTy)), SynExpr.Typed(innerExpr, _, _) when spatsL.IsEmpty -> | ||
| let retTy, _ = TcTypeAndRecover cenv NewTyparsOK CheckCxs ItemOccurrence.UseInType WarnOnIWSAM.Yes envinner tpenv retInfoTy | ||
| try UnifyTypes cenv envinner pat.Range retTy overallExprTy | ||
| with RecoverableException exn -> errorRecovery exn mRetTy | ||
| innerExpr | ||
| | _ -> |
There was a problem hiding this comment.
Consider
let a: T = bIf I understand it correctly, during the analysis the compiler rewrites it to
let a = (b: T)T may be a partially annotated type, e.g. _ list in the source, and then it can be inferred to something like int list after the type checking of the b expression. Can we propagate this inferred type here instead of type checking T from scratch? Would there be downsides?
There was a problem hiding this comment.
Sorry if I'm misunderstanding what you're saying here, but the problem this addresses is that when it's rewritten to let a = (b: T), if a is a pattern and b doesn't typecheck, it reports the annotated type of a as the type of b instead of the actual type of b (in the specific scenario where the type annotation doesn't fit the pattern).
Description
Previously, when a pattern on the LHS had a type annotation that didn't match the binding's type, a type error would report the annotation type as the type of the RHS. This PR separates it into two steps, so the binding type is checked against the annotation type, and then the RHS's type is checked against the binding's type. (Hopefully I worded this okay, I'm new to typechecking stuff.)
Fixes #18849
Checklist
Test cases added
Release notes entry updated: