Skip to content

Commit 0eeb4db

Browse files
And locals too
1 parent 0d63e61 commit 0eeb4db

6 files changed

+31
-6
lines changed

Diff for: compiler/rustc_hir_typeck/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ pub(crate) struct DependencyOnUnitNeverTypeFallback<'tcx> {
214214
pub(crate) enum SuggestAnnotation {
215215
Unit(Span),
216216
Path(Span),
217+
Local(Span),
217218
}
218219

219220
#[derive(Clone)]
@@ -240,6 +241,9 @@ impl Subdiagnostic for SuggestAnnotations {
240241
suggestions.push((span.shrink_to_lo(), "<() as ".to_string()));
241242
suggestions.push((span.shrink_to_hi(), ">".to_string()));
242243
}
244+
SuggestAnnotation::Local(span) => {
245+
suggestions.push((span, ": ()".to_string()));
246+
}
243247
}
244248
}
245249

Diff for: compiler/rustc_hir_typeck/src/fallback.rs

+13
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,19 @@ impl<'tcx> Visitor<'tcx> for VidVisitor<'_, 'tcx> {
620620
}
621621
hir::intravisit::walk_expr(self, expr)
622622
}
623+
624+
fn visit_local(&mut self, local: &'tcx hir::LetStmt<'tcx>) -> Self::Result {
625+
if let None = local.ty
626+
&& let ty = self.fcx.typeck_results.borrow().node_type(local.hir_id)
627+
&& let Some(vid) = self.fcx.root_vid(ty)
628+
&& self.reachable_vids.contains(&vid)
629+
{
630+
return ControlFlow::Break(errors::SuggestAnnotation::Local(
631+
local.pat.span.shrink_to_hi(),
632+
));
633+
}
634+
hir::intravisit::walk_local(self, local)
635+
}
623636
}
624637

625638
#[derive(Debug, Copy, Clone)]

Diff for: tests/ui/editions/never-type-fallback-breaking.e2021.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ LL | true => Default::default(),
1515
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
1616
help: use `()` annotations to avoid fallback changes
1717
|
18-
LL | true => <() as Default>::default(),
19-
| ++++++ +
18+
LL | let x: () = match true {
19+
| ++++
2020

2121
warning: this function depends on never type fallback being `()`
2222
--> $DIR/never-type-fallback-breaking.rs:27:1

Diff for: tests/ui/never_type/defaulted-never-note.nofallback.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ note: in edition 2024, the requirement `!: ImplementedForUnitButNotNever` will f
1313
LL | foo(_x);
1414
| ^^
1515
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
16+
help: use `()` annotations to avoid fallback changes
17+
|
18+
LL | let _x: () = return;
19+
| ++++
1620

1721
warning: 1 warning emitted
1822

Diff for: tests/ui/never_type/diverging-fallback-control-flow.nofallback.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ LL | x = UnitDefault::default();
1515
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
1616
help: use `()` annotations to avoid fallback changes
1717
|
18-
LL | x = <() as UnitDefault>::default();
19-
| ++++++ +
18+
LL | let x: ();
19+
| ++++
2020

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

4040
warning: 2 warnings emitted
4141

Diff for: tests/ui/never_type/diverging-fallback-unconstrained-return.nofallback.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ note: in edition 2024, the requirement `!: UnitReturn` will fail
1313
LL | let _ = if true { unconstrained_return() } else { panic!() };
1414
| ^^^^^^^^^^^^^^^^^^^^^^
1515
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
16+
help: use `()` annotations to avoid fallback changes
17+
|
18+
LL | let _: () = if true { unconstrained_return() } else { panic!() };
19+
| ++++
1620

1721
warning: 1 warning emitted
1822

0 commit comments

Comments
 (0)