Skip to content

Commit 9a9d0f4

Browse files
Improve spans for RPITIT object-safety errors
1 parent c4165f3 commit 9a9d0f4

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

compiler/rustc_middle/src/traits/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,13 @@ impl ObjectSafetyViolation {
924924
}
925925
ObjectSafetyViolation::Method(
926926
name,
927-
MethodViolationCode::ReferencesImplTraitInTrait,
927+
MethodViolationCode::ReferencesImplTraitInTrait(_),
928928
_,
929929
) => format!("method `{}` references an `impl Trait` type in its return type", name)
930930
.into(),
931+
ObjectSafetyViolation::Method(name, MethodViolationCode::AsyncFn, _) => {
932+
format!("method `{}` is `async`", name).into()
933+
}
931934
ObjectSafetyViolation::Method(
932935
name,
933936
MethodViolationCode::WhereClauseReferencesSelf,
@@ -1035,7 +1038,10 @@ pub enum MethodViolationCode {
10351038
ReferencesSelfOutput,
10361039

10371040
/// e.g., `fn foo(&self) -> impl Sized`
1038-
ReferencesImplTraitInTrait,
1041+
ReferencesImplTraitInTrait(Span),
1042+
1043+
/// e.g., `async fn foo(&self)`
1044+
AsyncFn,
10391045

10401046
/// e.g., `fn foo(&self) where Self: Clone`
10411047
WhereClauseReferencesSelf,

compiler/rustc_trait_selection/src/traits/object_safety.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ fn object_safety_violation_for_method(
375375
let span = match (&v, node) {
376376
(MethodViolationCode::ReferencesSelfInput(Some(span)), _) => *span,
377377
(MethodViolationCode::UndispatchableReceiver(Some(span)), _) => *span,
378+
(MethodViolationCode::ReferencesImplTraitInTrait(span), _) => *span,
378379
(MethodViolationCode::ReferencesSelfOutput, Some(node)) => {
379380
node.fn_decl().map_or(method.ident(tcx).span, |decl| decl.output.span())
380381
}
@@ -437,8 +438,8 @@ fn virtual_call_violation_for_method<'tcx>(
437438
if contains_illegal_self_type_reference(tcx, trait_def_id, sig.output()) {
438439
return Some(MethodViolationCode::ReferencesSelfOutput);
439440
}
440-
if contains_illegal_impl_trait_in_trait(tcx, sig.output()) {
441-
return Some(MethodViolationCode::ReferencesImplTraitInTrait);
441+
if let Some(code) = contains_illegal_impl_trait_in_trait(tcx, method.def_id, sig.output()) {
442+
return Some(code);
442443
}
443444

444445
// We can't monomorphize things like `fn foo<A>(...)`.
@@ -864,16 +865,24 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<'tcx>>(
864865

865866
pub fn contains_illegal_impl_trait_in_trait<'tcx>(
866867
tcx: TyCtxt<'tcx>,
868+
fn_def_id: DefId,
867869
ty: ty::Binder<'tcx, Ty<'tcx>>,
868-
) -> bool {
870+
) -> Option<MethodViolationCode> {
871+
// This would be caught below, but rendering the error as a separate
872+
// `async-specific` message is better.
873+
if tcx.asyncness(fn_def_id).is_async() {
874+
return Some(MethodViolationCode::AsyncFn);
875+
}
876+
869877
// FIXME(RPITIT): Perhaps we should use a visitor here?
870-
ty.skip_binder().walk().any(|arg| {
878+
ty.skip_binder().walk().find_map(|arg| {
871879
if let ty::GenericArgKind::Type(ty) = arg.unpack()
872880
&& let ty::Projection(proj) = ty.kind()
881+
&& tcx.def_kind(proj.item_def_id) == DefKind::ImplTraitPlaceholder
873882
{
874-
tcx.def_kind(proj.item_def_id) == DefKind::ImplTraitPlaceholder
883+
Some(MethodViolationCode::ReferencesImplTraitInTrait(tcx.def_span(proj.item_def_id)))
875884
} else {
876-
false
885+
None
877886
}
878887
})
879888
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2021
2+
3+
#![feature(async_fn_in_trait)]
4+
//~^ WARN the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
5+
6+
trait Foo {
7+
async fn foo(&self);
8+
}
9+
10+
fn main() {
11+
let x: &dyn Foo = todo!();
12+
//~^ ERROR the trait `Foo` cannot be made into an object
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
warning: the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/object-safety.rs:3:12
3+
|
4+
LL | #![feature(async_fn_in_trait)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0038]: the trait `Foo` cannot be made into an object
11+
--> $DIR/object-safety.rs:11:12
12+
|
13+
LL | let x: &dyn Foo = todo!();
14+
| ^^^^^^^^ `Foo` cannot be made into an object
15+
|
16+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
17+
--> $DIR/object-safety.rs:7:14
18+
|
19+
LL | trait Foo {
20+
| --- this trait cannot be made into an object...
21+
LL | async fn foo(&self);
22+
| ^^^ ...because method `foo` is `async`
23+
= help: consider moving `foo` to another trait
24+
25+
error: aborting due to previous error; 1 warning emitted
26+
27+
For more information about this error, try `rustc --explain E0038`.

src/test/ui/impl-trait/in-trait/object-safety.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ LL | let i = Box::new(42_u32) as Box<dyn Foo>;
55
| ^^^^^^^^^^^^ `Foo` cannot be made into an object
66
|
77
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
8-
--> $DIR/object-safety.rs:7:8
8+
--> $DIR/object-safety.rs:7:22
99
|
1010
LL | trait Foo {
1111
| --- this trait cannot be made into an object...
1212
LL | fn baz(&self) -> impl Debug;
13-
| ^^^ ...because method `baz` references an `impl Trait` type in its return type
13+
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
1414
= help: consider moving `baz` to another trait
1515

1616
error[E0038]: the trait `Foo` cannot be made into an object
@@ -20,12 +20,12 @@ LL | let s = i.baz();
2020
| ^^^^^^^ `Foo` cannot be made into an object
2121
|
2222
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
23-
--> $DIR/object-safety.rs:7:8
23+
--> $DIR/object-safety.rs:7:22
2424
|
2525
LL | trait Foo {
2626
| --- this trait cannot be made into an object...
2727
LL | fn baz(&self) -> impl Debug;
28-
| ^^^ ...because method `baz` references an `impl Trait` type in its return type
28+
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
2929
= help: consider moving `baz` to another trait
3030

3131
error[E0038]: the trait `Foo` cannot be made into an object
@@ -35,12 +35,12 @@ LL | let i = Box::new(42_u32) as Box<dyn Foo>;
3535
| ^^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
3636
|
3737
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
38-
--> $DIR/object-safety.rs:7:8
38+
--> $DIR/object-safety.rs:7:22
3939
|
4040
LL | trait Foo {
4141
| --- this trait cannot be made into an object...
4242
LL | fn baz(&self) -> impl Debug;
43-
| ^^^ ...because method `baz` references an `impl Trait` type in its return type
43+
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
4444
= help: consider moving `baz` to another trait
4545
= note: required for `Box<u32>` to implement `CoerceUnsized<Box<dyn Foo>>`
4646
= note: required by cast to type `Box<dyn Foo>`

0 commit comments

Comments
 (0)