Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Only suggest unit for unit fallback when spans dont come from macro e…
…xpansion
  • Loading branch information
compiler-errors committed Feb 5, 2025
commit cac63ffc0fe4d963f902311d5938e03a0a8437aa
12 changes: 10 additions & 2 deletions compiler/rustc_hir_typeck/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
if let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(inf_id)
&& let Some(vid) = self.fcx.root_vid(ty)
&& self.reachable_vids.contains(&vid)
&& inf_span.can_be_used_for_suggestions()
{
return ControlFlow::Break(errors::SuggestAnnotation::Unit(inf_span));
}
Expand All @@ -662,7 +663,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
&mut self,
qpath: &'tcx rustc_hir::QPath<'tcx>,
id: HirId,
_span: Span,
span: Span,
) -> Self::Result {
let arg_segment = match qpath {
hir::QPath::Resolved(_, path) => {
Expand All @@ -674,7 +675,9 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
}
};
// Alternatively, try to turbofish `::<_, (), _>`.
if let Some(def_id) = self.fcx.typeck_results.borrow().qpath_res(qpath, id).opt_def_id() {
if let Some(def_id) = self.fcx.typeck_results.borrow().qpath_res(qpath, id).opt_def_id()
&& span.can_be_used_for_suggestions()
{
self.suggest_for_segment(arg_segment, def_id, id)?;
}
hir::intravisit::walk_qpath(self, qpath, id)
Expand All @@ -691,17 +694,21 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
&& let Some(vid) = self.fcx.root_vid(self_ty)
&& self.reachable_vids.contains(&vid)
&& let [.., trait_segment, _method_segment] = path.segments
&& expr.span.can_be_used_for_suggestions()
{
let span = path.span.shrink_to_lo().to(trait_segment.ident.span);
return ControlFlow::Break(errors::SuggestAnnotation::Path(span));
}

// Or else, try suggesting turbofishing the method args.
if let hir::ExprKind::MethodCall(segment, ..) = expr.kind
&& let Some(def_id) =
self.fcx.typeck_results.borrow().type_dependent_def_id(expr.hir_id)
&& expr.span.can_be_used_for_suggestions()
{
self.suggest_for_segment(segment, def_id, expr.hir_id)?;
}

hir::intravisit::walk_expr(self, expr)
}

Expand All @@ -712,6 +719,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
&& let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(local.hir_id)
&& let Some(vid) = self.fcx.root_vid(ty)
&& self.reachable_vids.contains(&vid)
&& local.span.can_be_used_for_suggestions()
{
return ControlFlow::Break(errors::SuggestAnnotation::Local(
local.pat.span.shrink_to_hi(),
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/never_type/dont-suggest-turbofish-from-expansion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![deny(dependency_on_unit_never_type_fallback)]

fn create_ok_default<C>() -> Result<C, ()>
where
C: Default,
{
Ok(C::default())
}

fn main() -> Result<(), ()> {
//~^ ERROR this function depends on never type fallback being `()`
//~| WARN this was previously accepted by the compiler but is being phased out
let (returned_value, _) = (|| {
let created = create_ok_default()?;
Ok((created, ()))
})()?;

let _ = format_args!("{:?}", returned_value);
Ok(())
}
22 changes: 22 additions & 0 deletions tests/ui/never_type/dont-suggest-turbofish-from-expansion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: this function depends on never type fallback being `()`
--> $DIR/dont-suggest-turbofish-from-expansion.rs:10:1
|
LL | fn main() -> Result<(), ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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!
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>
= help: specify the types explicitly
note: in edition 2024, the requirement `!: Default` will fail
--> $DIR/dont-suggest-turbofish-from-expansion.rs:14:23
|
LL | let created = create_ok_default()?;
| ^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/dont-suggest-turbofish-from-expansion.rs:1:9
|
LL | #![deny(dependency_on_unit_never_type_fallback)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

warning: 10 warnings emitted

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

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