feat(diagnostics): emit error for infer vars in non-inference contexts#22469
Conversation
b6099e3 to
627342b
Compare
| Some(infer_vars) => infer_vars.next_ty_var(span), | ||
| None => { | ||
| // FIXME: Emit an error: no infer vars allowed here. | ||
| if let Span::TypeRefId(type_ref) = span { |
There was a problem hiding this comment.
Handling them just for TypeRefs is not enough. You need to handle them for any span that isn't dummy. TyLoweringDiagnostic cannot support this currently, so you'll need to change it (one possibility is inlining the TypeRefId into the enum).
86db4ed to
ac3b3b0
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
| let Ok(source) = source_map.type_syntax(diag.source) else { | ||
| stdx::never!("error on synthetic type syntax"); | ||
| return None; | ||
| let span_syntax = |source| match source { |
There was a problem hiding this comment.
We have X_syntax closures in inference_diagnostic(), please make them into associated functions and call them from both places. You can still also have closure wrappers since the associated functions will need to take the source map.
| pub(crate) mod unresolved_module; | ||
| pub(crate) mod unused_must_use; | ||
| pub(crate) mod unused_variables; | ||
|
|
There was a problem hiding this comment.
Revert this newline removal.
| source_map | ||
| .expr_syntax(expr) | ||
| Self::expr_syntax(expr, source_map) | ||
| .inspect_err(|_| stdx::never!("inference diagnostic in desugared expr")) |
There was a problem hiding this comment.
I meant that you will also put the inspect_err(...).ok() in the associated function, currently it doesn't carry its weight.
| fn span_syntax( | ||
| span: hir_ty::Span, | ||
| source_map: &ExpressionStoreSourceMap, | ||
| ) -> Result<InFile<SyntaxNodePtr>, SyntheticSyntax> { |
There was a problem hiding this comment.
This should also return Option. In fact we already have a span_syntax() closure in inference_diagnostic() that you should copy (and move).
| let ast::Type::PathType(syntax) = syntax() else { return None }; | ||
| Some(match diag { | ||
| TyLoweringDiagnostic::PathDiagnostic { source, diag } => { | ||
| let Ok(source) = source_map.type_syntax(*source) else { |
There was a problem hiding this comment.
Use Self::type_syntax() here.
Address the FIXMEs in crates/hir-ty/src/lower.rs where next_ty_var, next_const_var, and next_region_var silently returned error types without emitting diagnostics when inference variables are not allowed (e.g., `_` in type aliases, consts, statics, struct fields). - Add InferVarsNotAllowed variant to TyLoweringDiagnostic enum - Call push_diagnostic in the three next_*_var functions for any non-dummy span - Convert TyLoweringDiagnostic from struct+kind to flat enum with per-variant source (TypeRefId for PathDiagnostic, Span for InferVarsNotAllowed) - Add span_syntax helper resolving all Span variants to AST nodes - Extract expr_syntax/pat_syntax/type_syntax/span_syntax closures from inference_diagnostic into associated functions on AnyDiagnostic - Add InferVarsNotAllowed HIR diagnostic struct with InFile<SyntaxNodePtr> node - Add ide-diagnostics handler with E0121 error code and tests
27803fc to
725cd24
Compare
Address the FIXME in
crates/hir-ty/src/lower.rs.Previously
next_ty_var,next_const_var, andnext_region_varsilently returned error types when inference variables were forbidden.
This PR:
InferVarsNotAllowedtoTyLoweringDiagnosticKindPart of #22140