Skip to content

Commit 06eebbe

Browse files
authored
Rollup merge of #114146 - compiler-errors:dont-report-rpitit-name, r=spastorino
Skip reporting item name when checking RPITIT GAT's associated type bounds hold Doesn't really make sense to label an item that has a name that users can't really mention. Fixes #114145. Also fixes #113794. r? `@spastorino`
2 parents 3aa8da1 + ea2f8b3 commit 06eebbe

File tree

6 files changed

+121
-8
lines changed

6 files changed

+121
-8
lines changed

compiler/rustc_infer/src/infer/error_reporting/note.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
243243
}
244244
infer::CheckAssociatedTypeBounds { impl_item_def_id, trait_item_def_id, parent } => {
245245
let mut err = self.report_concrete_failure(*parent, sub, sup);
246-
let trait_item_span = self.tcx.def_span(trait_item_def_id);
247-
let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
248-
err.span_label(
249-
trait_item_span,
250-
format!("definition of `{}` from trait", item_name),
251-
);
246+
247+
// Don't mention the item name if it's an RPITIT, since that'll just confuse
248+
// folks.
249+
if !self.tcx.is_impl_trait_in_trait(impl_item_def_id.to_def_id()) {
250+
let trait_item_span = self.tcx.def_span(trait_item_def_id);
251+
let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
252+
err.span_label(
253+
trait_item_span,
254+
format!("definition of `{}` from trait", item_name),
255+
);
256+
}
257+
252258
self.suggest_copy_trait_method_bounds(
253259
trait_item_def_id,
254260
impl_item_def_id,

compiler/rustc_ty_utils/src/assoc.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,16 @@ fn associated_type_for_impl_trait_in_impl(
346346
) -> LocalDefId {
347347
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
348348

349-
// FIXME fix the span, we probably want the def_id of the return type of the function
350-
let span = tcx.def_span(impl_fn_def_id);
349+
let decl = tcx
350+
.hir()
351+
.find_by_def_id(impl_fn_def_id)
352+
.expect("expected item")
353+
.fn_decl()
354+
.expect("expected decl");
355+
let span = match decl.output {
356+
hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id),
357+
hir::FnRetTy::Return(ty) => ty.span,
358+
};
351359
let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy);
352360

353361
let local_def_id = impl_assoc_ty.def_id();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// issue: 114146
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
5+
trait Foo {
6+
fn bar<'other: 'a>() -> impl Sized + 'a {}
7+
//~^ ERROR use of undeclared lifetime name `'a`
8+
//~| ERROR use of undeclared lifetime name `'a`
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0261]: use of undeclared lifetime name `'a`
2+
--> $DIR/bad-item-bound-within-rpitit-2.rs:6:20
3+
|
4+
LL | fn bar<'other: 'a>() -> impl Sized + 'a {}
5+
| ^^ undeclared lifetime
6+
|
7+
help: consider introducing lifetime `'a` here
8+
|
9+
LL | fn bar<'a, 'other: 'a>() -> impl Sized + 'a {}
10+
| +++
11+
help: consider introducing lifetime `'a` here
12+
|
13+
LL | trait Foo<'a> {
14+
| ++++
15+
16+
error[E0261]: use of undeclared lifetime name `'a`
17+
--> $DIR/bad-item-bound-within-rpitit-2.rs:6:42
18+
|
19+
LL | fn bar<'other: 'a>() -> impl Sized + 'a {}
20+
| ^^ undeclared lifetime
21+
|
22+
help: consider introducing lifetime `'a` here
23+
|
24+
LL | fn bar<'a, 'other: 'a>() -> impl Sized + 'a {}
25+
| +++
26+
help: consider introducing lifetime `'a` here
27+
|
28+
LL | trait Foo<'a> {
29+
| ++++
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0261`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// issue: 114145
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
5+
trait Iterable {
6+
type Item<'a>
7+
where
8+
Self: 'a;
9+
10+
fn iter(&self) -> impl '_ + Iterator<Item = Self::Item<'_>>;
11+
}
12+
13+
impl<'a, I: 'a + Iterable> Iterable for &'a I {
14+
type Item<'b> = I::Item<'a>
15+
where
16+
'b: 'a;
17+
//~^ ERROR impl has stricter requirements than trait
18+
19+
fn iter(&self) -> impl 'a + Iterator<Item = I::Item<'a>> {
20+
//~^ ERROR the type `&'a I` does not fulfill the required lifetime
21+
(*self).iter()
22+
}
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0276]: impl has stricter requirements than trait
2+
--> $DIR/bad-item-bound-within-rpitit.rs:16:13
3+
|
4+
LL | type Item<'a>
5+
| ------------- definition of `Item` from trait
6+
...
7+
LL | 'b: 'a;
8+
| ^^ impl has extra requirement `'b: 'a`
9+
|
10+
help: copy the `where` clause predicates from the trait
11+
|
12+
LL | where Self: 'b;
13+
| ~~~~~~~~~~~~~~
14+
15+
error[E0477]: the type `&'a I` does not fulfill the required lifetime
16+
--> $DIR/bad-item-bound-within-rpitit.rs:19:23
17+
|
18+
LL | fn iter(&self) -> impl 'a + Iterator<Item = I::Item<'a>> {
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
note: type must outlive the anonymous lifetime as defined here
22+
--> $DIR/bad-item-bound-within-rpitit.rs:10:28
23+
|
24+
LL | fn iter(&self) -> impl '_ + Iterator<Item = Self::Item<'_>>;
25+
| ^^
26+
27+
error: aborting due to 2 previous errors
28+
29+
Some errors have detailed explanations: E0276, E0477.
30+
For more information about an error, try `rustc --explain E0276`.

0 commit comments

Comments
 (0)