Skip to content

Commit

Permalink
And locals too
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Nov 1, 2024
1 parent ea4fb7c commit c930bba
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub(crate) struct DependencyOnUnitNeverTypeFallback<'tcx> {
pub(crate) enum SuggestAnnotation {
Unit(Span),
Path(Span),
Local(Span),
}

#[derive(Clone)]
Expand All @@ -240,6 +241,9 @@ impl Subdiagnostic for SuggestAnnotations {
suggestions.push((span.shrink_to_lo(), "<() as ".to_string()));
suggestions.push((span.shrink_to_hi(), ">".to_string()));
}
SuggestAnnotation::Local(span) => {
suggestions.push((span, ": ()".to_string()));
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_hir_typeck/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,19 @@ impl<'tcx> Visitor<'tcx> for VidVisitor<'_, 'tcx> {
}
hir::intravisit::walk_expr(self, expr)
}

fn visit_local(&mut self, local: &'tcx hir::LetStmt<'tcx>) -> Self::Result {
if let None = local.ty
&& let ty = self.fcx.typeck_results.borrow().node_type(local.hir_id)
&& let Some(vid) = self.fcx.root_vid(ty)
&& self.reachable_vids.contains(&vid)
{
return ControlFlow::Break(errors::SuggestAnnotation::Local(
local.pat.span.shrink_to_hi(),
));
}
hir::intravisit::walk_local(self, local)
}
}

#[derive(Debug, Copy, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/editions/never-type-fallback-breaking.e2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ LL | true => Default::default(),
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
|
LL | true => <() as Default>::default(),
| ++++++ +
LL | let x: () = match true {
| ++++

warning: this function depends on never type fallback being `()`
--> $DIR/never-type-fallback-breaking.rs:27:1
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/never_type/defaulted-never-note.nofallback.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ note: in edition 2024, the requirement `!: ImplementedForUnitButNotNever` will f
LL | foo(_x);
| ^^
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
|
LL | let _x: () = return;
| ++++

warning: 1 warning emitted

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ LL | x = UnitDefault::default();
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
|
LL | x = <() as UnitDefault>::default();
| ++++++ +
LL | let x: ();
| ++++

warning: this function depends on never type fallback being `()`
--> $DIR/diverging-fallback-control-flow.rs:42:1
Expand All @@ -34,8 +34,8 @@ LL | x = UnitDefault::default();
| ^^^^^^^^^^^^^^^^^^^^^^
help: use `()` annotations to avoid fallback changes
|
LL | x = <() as UnitDefault>::default();
| ++++++ +
LL | let x: ();
| ++++

warning: 2 warnings emitted

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ note: in edition 2024, the requirement `!: UnitReturn` will fail
LL | let _ = if true { unconstrained_return() } else { panic!() };
| ^^^^^^^^^^^^^^^^^^^^^^
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
|
LL | let _: () = if true { unconstrained_return() } else { panic!() };
| ++++

warning: 1 warning emitted

0 comments on commit c930bba

Please sign in to comment.