Skip to content

feat(diagnostics): emit error for infer vars in non-inference contexts#22469

Merged
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Vlm326:diagnostics/no-infer-vars
Jun 3, 2026
Merged

feat(diagnostics): emit error for infer vars in non-inference contexts#22469
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Vlm326:diagnostics/no-infer-vars

Conversation

@Vlm326
Copy link
Copy Markdown

@Vlm326 Vlm326 commented May 27, 2026

Address the FIXME in crates/hir-ty/src/lower.rs.

Previously next_ty_var, next_const_var, and next_region_var
silently returned error types when inference variables were forbidden.

This PR:

  • adds InferVarsNotAllowed to TyLoweringDiagnosticKind
  • emits diagnostics from lowering
  • exposes the diagnostic through HIR
  • adds an IDE handler and tests

Part of #22140

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 27, 2026
@Vlm326 Vlm326 force-pushed the diagnostics/no-infer-vars branch 3 times, most recently from b6099e3 to 627342b Compare May 27, 2026 15:12
Comment thread crates/hir-ty/src/lower.rs Outdated
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 {
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 May 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

View changes since the review

@Vlm326 Vlm326 force-pushed the diagnostics/no-infer-vars branch from 86db4ed to ac3b3b0 Compare June 2, 2026 16:08
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 2, 2026

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.

@Vlm326 Vlm326 requested a review from ChayimFriedman2 June 2, 2026 16:21
Comment thread crates/hir/src/diagnostics.rs Outdated
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 {
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

pub(crate) mod unresolved_module;
pub(crate) mod unused_must_use;
pub(crate) mod unused_variables;

Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this newline removal.

View changes since the review

Comment thread crates/hir/src/diagnostics.rs Outdated
source_map
.expr_syntax(expr)
Self::expr_syntax(expr, source_map)
.inspect_err(|_| stdx::never!("inference diagnostic in desugared expr"))
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that you will also put the inspect_err(...).ok() in the associated function, currently it doesn't carry its weight.

View changes since the review

Comment thread crates/hir/src/diagnostics.rs Outdated
fn span_syntax(
span: hir_ty::Span,
source_map: &ExpressionStoreSourceMap,
) -> Result<InFile<SyntaxNodePtr>, SyntheticSyntax> {
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also return Option. In fact we already have a span_syntax() closure in inference_diagnostic() that you should copy (and move).

View changes since the review

Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And please squash.

View changes since this review

Comment thread crates/hir/src/diagnostics.rs Outdated
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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@Vlm326 Vlm326 force-pushed the diagnostics/no-infer-vars branch from 27803fc to 725cd24 Compare June 3, 2026 15:24
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Jun 3, 2026
Merged via the queue into rust-lang:master with commit a0a5c97 Jun 3, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants