Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a7fe30d

Browse files
authoredJul 10, 2024
Rollup merge of #127094 - Borgerr:E0191-suggestion-correction, r=fmease
E0191 suggestion correction, inserts turbofish closes #91997
2 parents d17e0cf + 7c88bda commit a7fe30d

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
@@ -11,9 +11,9 @@ use rustc_errors::MultiSpan;
1111
use rustc_errors::{
1212
codes::*, pluralize, struct_span_code_err, Applicability, Diag, ErrorGuaranteed,
1313
};
14-
use rustc_hir as hir;
1514
use rustc_hir::def::{DefKind, Res};
1615
use rustc_hir::def_id::{DefId, LocalDefId};
16+
use rustc_hir::{self as hir, Node};
1717
use rustc_middle::bug;
1818
use rustc_middle::query::Key;
1919
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
@@ -740,7 +740,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
740740
if object_safety_violations {
741741
return;
742742
}
743+
744+
// related to issue #91997, turbofishes added only when in an expr or pat
745+
let mut in_expr_or_pat = false;
743746
if let ([], [bound]) = (&potential_assoc_types[..], &trait_bounds) {
747+
let grandparent = tcx.parent_hir_node(tcx.parent_hir_id(bound.trait_ref.hir_ref_id));
748+
in_expr_or_pat = match grandparent {
749+
Node::Expr(_) | Node::Pat(_) => true,
750+
_ => false,
751+
};
744752
match bound.trait_ref.path.segments {
745753
// FIXME: `trait_ref.path.span` can point to a full path with multiple
746754
// segments, even though `trait_ref.path.segments` is of length `1`. Work
@@ -896,6 +904,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
896904
// `Trait<'a, Item = Type>` while accounting for the `<'a>` in the
897905
// suggestion.
898906
format!("{}, {}>", &snippet[..snippet.len() - 1], types.join(", "))
907+
} else if in_expr_or_pat {
908+
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
909+
// least we can clue them to the correct syntax `Iterator::<Item = Type>`.
910+
format!("{}::<{}>", snippet, types.join(", "))
899911
} else {
900912
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
901913
// 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)
Failed to load comments.