Skip to content

Commit 7489e56

Browse files
authored
Rollup merge of #136598 - compiler-errors:unit-fallback, r=WaffleLapkin
Fix suggestion for `dependency_on_unit_never_type_fallback` involving closures + format args expansions fixes #136562 r? wafflelapkin or reassign
2 parents 20f9e97 + eb9bba8 commit 7489e56

5 files changed

+62
-10
lines changed

compiler/rustc_hir_typeck/src/fallback.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
651651
if let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(inf_id)
652652
&& let Some(vid) = self.fcx.root_vid(ty)
653653
&& self.reachable_vids.contains(&vid)
654+
&& inf_span.can_be_used_for_suggestions()
654655
{
655656
return ControlFlow::Break(errors::SuggestAnnotation::Unit(inf_span));
656657
}
@@ -662,7 +663,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
662663
&mut self,
663664
qpath: &'tcx rustc_hir::QPath<'tcx>,
664665
id: HirId,
665-
_span: Span,
666+
span: Span,
666667
) -> Self::Result {
667668
let arg_segment = match qpath {
668669
hir::QPath::Resolved(_, path) => {
@@ -674,13 +675,21 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
674675
}
675676
};
676677
// Alternatively, try to turbofish `::<_, (), _>`.
677-
if let Some(def_id) = self.fcx.typeck_results.borrow().qpath_res(qpath, id).opt_def_id() {
678+
if let Some(def_id) = self.fcx.typeck_results.borrow().qpath_res(qpath, id).opt_def_id()
679+
&& span.can_be_used_for_suggestions()
680+
{
678681
self.suggest_for_segment(arg_segment, def_id, id)?;
679682
}
680683
hir::intravisit::walk_qpath(self, qpath, id)
681684
}
682685

683686
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Self::Result {
687+
if let hir::ExprKind::Closure(&hir::Closure { body, .. })
688+
| hir::ExprKind::ConstBlock(hir::ConstBlock { body, .. }) = expr.kind
689+
{
690+
self.visit_body(self.fcx.tcx.hir().body(body))?;
691+
}
692+
684693
// Try to suggest adding an explicit qself `()` to a trait method path.
685694
// i.e. changing `Default::default()` to `<() as Default>::default()`.
686695
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
@@ -691,17 +700,21 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
691700
&& let Some(vid) = self.fcx.root_vid(self_ty)
692701
&& self.reachable_vids.contains(&vid)
693702
&& let [.., trait_segment, _method_segment] = path.segments
703+
&& expr.span.can_be_used_for_suggestions()
694704
{
695705
let span = path.span.shrink_to_lo().to(trait_segment.ident.span);
696706
return ControlFlow::Break(errors::SuggestAnnotation::Path(span));
697707
}
708+
698709
// Or else, try suggesting turbofishing the method args.
699710
if let hir::ExprKind::MethodCall(segment, ..) = expr.kind
700711
&& let Some(def_id) =
701712
self.fcx.typeck_results.borrow().type_dependent_def_id(expr.hir_id)
713+
&& expr.span.can_be_used_for_suggestions()
702714
{
703715
self.suggest_for_segment(segment, def_id, expr.hir_id)?;
704716
}
717+
705718
hir::intravisit::walk_expr(self, expr)
706719
}
707720

@@ -712,6 +725,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
712725
&& let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(local.hir_id)
713726
&& let Some(vid) = self.fcx.root_vid(ty)
714727
&& self.reachable_vids.contains(&vid)
728+
&& local.span.can_be_used_for_suggestions()
715729
{
716730
return ControlFlow::Break(errors::SuggestAnnotation::Local(
717731
local.pat.span.shrink_to_hi(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![deny(dependency_on_unit_never_type_fallback)]
2+
3+
fn create_ok_default<C>() -> Result<C, ()>
4+
where
5+
C: Default,
6+
{
7+
Ok(C::default())
8+
}
9+
10+
fn main() -> Result<(), ()> {
11+
//~^ ERROR this function depends on never type fallback being `()`
12+
//~| WARN this was previously accepted by the compiler but is being phased out
13+
let (returned_value, _) = (|| {
14+
let created = create_ok_default()?;
15+
Ok((created, ()))
16+
})()?;
17+
18+
let _ = format_args!("{:?}", returned_value);
19+
Ok(())
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: this function depends on never type fallback being `()`
2+
--> $DIR/dont-suggest-turbofish-from-expansion.rs:10:1
3+
|
4+
LL | fn main() -> Result<(), ()> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
8+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>
9+
= help: specify the types explicitly
10+
note: in edition 2024, the requirement `!: Default` will fail
11+
--> $DIR/dont-suggest-turbofish-from-expansion.rs:14:23
12+
|
13+
LL | let created = create_ok_default()?;
14+
| ^^^^^^^^^^^^^^^^^^^
15+
note: the lint level is defined here
16+
--> $DIR/dont-suggest-turbofish-from-expansion.rs:1:9
17+
|
18+
LL | #![deny(dependency_on_unit_never_type_fallback)]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
help: use `()` annotations to avoid fallback changes
21+
|
22+
LL | let created: () = create_ok_default()?;
23+
| ++++
24+
25+
error: aborting due to 1 previous error
26+

tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2015.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ LL | msg_send!();
130130
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>
131131
= help: specify the type explicitly
132132
= note: this warning originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
133-
help: use `()` annotations to avoid fallback changes
134-
|
135-
LL | match send_message::<() /* ?0 */>() {
136-
| ~~
137133

138134
warning: 10 warnings emitted
139135

tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2024.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ LL | msg_send!();
130130
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>
131131
= help: specify the type explicitly
132132
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
133-
help: use `()` annotations to avoid fallback changes
134-
|
135-
LL | match send_message::<() /* ?0 */>() {
136-
| ~~
137133

138134
warning: the type `!` does not permit zero-initialization
139135
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:12:18

0 commit comments

Comments
 (0)