Skip to content

Commit 7c88bda

Browse files
committed
E0191 suggestion correction, inserts turbofish without dyn (#91997)
1 parent 289deb9 commit 7c88bda

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+13-1
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>`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
trait MyIterator : Iterator {}
2+
3+
fn main() {
4+
let _ = MyIterator::next;
5+
}
6+
//~^^ ERROR the value of the associated type `Item` in `Iterator` must be specified [E0191]
7+
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
8+
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
warning: trait objects without an explicit `dyn` are deprecated
2+
--> $DIR/dynless-turbofish-e0191-issue-91997.rs:4:13
3+
|
4+
LL | let _ = MyIterator::next;
5+
| ^^^^^^^^^^
6+
|
7+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
8+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
9+
= note: `#[warn(bare_trait_objects)]` on by default
10+
help: if this is an object-safe trait, use `dyn`
11+
|
12+
LL | let _ = <dyn MyIterator>::next;
13+
| ++++ +
14+
15+
error[E0191]: the value of the associated type `Item` in `Iterator` must be specified
16+
--> $DIR/dynless-turbofish-e0191-issue-91997.rs:4:13
17+
|
18+
LL | let _ = MyIterator::next;
19+
| ^^^^^^^^^^ help: specify the associated type: `MyIterator::<Item = Type>`
20+
21+
error: aborting due to 1 previous error; 1 warning emitted
22+
23+
For more information about this error, try `rustc --explain E0191`.

tests/ui/issues/issue-23024.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ error[E0191]: the value of the associated type `Output` in `FnOnce` must be spec
2323
--> $DIR/issue-23024.rs:8:39
2424
|
2525
LL | println!("{:?}",(vfnfer[0] as dyn Fn)(3));
26-
| ^^ help: specify the associated type: `Fn<Output = Type>`
26+
| ^^ help: specify the associated type: `Fn::<Output = Type>`
2727

2828
error: aborting due to 3 previous errors
2929

tests/ui/issues/issue-28344.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0191]: the value of the associated type `Output` in `BitXor` must be spec
1616
--> $DIR/issue-28344.rs:4:17
1717
|
1818
LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
19-
| ^^^^^^ help: specify the associated type: `BitXor<Output = Type>`
19+
| ^^^^^^ help: specify the associated type: `BitXor::<Output = Type>`
2020

2121
error[E0599]: no function or associated item named `bitor` found for trait object `dyn BitXor<_>` in the current scope
2222
--> $DIR/issue-28344.rs:4:25
@@ -44,7 +44,7 @@ error[E0191]: the value of the associated type `Output` in `BitXor` must be spec
4444
--> $DIR/issue-28344.rs:10:13
4545
|
4646
LL | let g = BitXor::bitor;
47-
| ^^^^^^ help: specify the associated type: `BitXor<Output = Type>`
47+
| ^^^^^^ help: specify the associated type: `BitXor::<Output = Type>`
4848

4949
error[E0599]: no function or associated item named `bitor` found for trait object `dyn BitXor<_>` in the current scope
5050
--> $DIR/issue-28344.rs:10:21

0 commit comments

Comments
 (0)