Skip to content

Commit 6f5d7b3

Browse files
committed
E0191 suggestion includes turbofish when dyn absent (#91997)
1 parent 289deb9 commit 6f5d7b3

File tree

1 file changed

+13
-1
lines changed
  • compiler/rustc_hir_analysis/src/hir_ty_lowering

1 file changed

+13
-1
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use rustc_errors::MultiSpan;
1212
use rustc_errors::{
1313
codes::*, pluralize, struct_span_code_err, Applicability, Diag, ErrorGuaranteed,
1414
};
15-
use rustc_hir as hir;
1615
use rustc_hir::def::{DefKind, Res};
1716
use rustc_hir::def_id::{DefId, LocalDefId};
17+
use rustc_hir::{self as hir, Node};
1818
use rustc_middle::bug;
1919
use rustc_middle::query::Key;
2020
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
@@ -745,7 +745,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
745745
if object_safety_violations {
746746
return;
747747
}
748+
749+
// related to issue #91997, turbofishes added only when in an expr or pat
750+
let mut in_expr_or_pat = false;
748751
if let ([], [bound]) = (&potential_assoc_types[..], &trait_bounds) {
752+
let grandparent = tcx.parent_hir_node(tcx.parent_hir_id(bound.trait_ref.hir_ref_id));
753+
in_expr_or_pat = match grandparent {
754+
Node::Expr(_) | Node::Pat(_) => true,
755+
_ => false,
756+
};
749757
match bound.trait_ref.path.segments {
750758
// FIXME: `trait_ref.path.span` can point to a full path with multiple
751759
// segments, even though `trait_ref.path.segments` is of length `1`. Work
@@ -901,6 +909,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
901909
// `Trait<'a, Item = Type>` while accounting for the `<'a>` in the
902910
// suggestion.
903911
format!("{}, {}>", &snippet[..snippet.len() - 1], types.join(", "))
912+
} else if in_expr_or_pat {
913+
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
914+
// least we can clue them to the correct syntax `Iterator::<Item = Type>`.
915+
format!("{}::<{}>", snippet, types.join(", "))
904916
} else {
905917
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
906918
// least we can clue them to the correct syntax `Iterator<Item = Type>`.

0 commit comments

Comments
 (0)