Skip to content

Use more detailed spans in dyn compat errors within bodies #141580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt, walk_generics};
use rustc_hir::{self as hir, GenericParamKind, HirId, Node, PreciseCapturingArgKind};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause;
use rustc_infer::traits::{DynCompatibilityViolation, ObligationCause};
use rustc_middle::hir::nested_filter;
use rustc_middle::query::Providers;
use rustc_middle::ty::util::{Discr, IntTypeExt};
Expand All @@ -40,7 +40,7 @@ use rustc_middle::{bug, span_bug};
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
use rustc_trait_selection::error_reporting::traits::suggestions::NextTypeParamName;
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::ObligationCtxt;
use rustc_trait_selection::traits::{ObligationCtxt, hir_ty_lowering_dyn_compatibility_violations};
use tracing::{debug, instrument};

use crate::errors;
Expand Down Expand Up @@ -625,6 +625,10 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {

(input_tys, output_ty)
}

fn dyn_compatibility_violations(&self, trait_def_id: DefId) -> Vec<DynCompatibilityViolation> {
hir_ty_lowering_dyn_compatibility_violations(self.tcx, trait_def_id)
}
}

/// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_middle::ty::{
};
use rustc_span::{ErrorGuaranteed, Span};
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
use rustc_trait_selection::traits::{self, hir_ty_lowering_dyn_compatibility_violations};
use rustc_trait_selection::traits;
use smallvec::{SmallVec, smallvec};
use tracing::{debug, instrument};

Expand Down Expand Up @@ -97,8 +97,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
// to avoid ICEs.
for (clause, span) in user_written_bounds {
if let Some(trait_pred) = clause.as_trait_clause() {
let violations =
hir_ty_lowering_dyn_compatibility_violations(tcx, trait_pred.def_id());
let violations = self.dyn_compatibility_violations(trait_pred.def_id());
if !violations.is_empty() {
let reported = report_dyn_incompatibility(
tcx,
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause;
use rustc_infer::traits::{DynCompatibilityViolation, ObligationCause};
use rustc_middle::middle::stability::AllowUnstable;
use rustc_middle::mir::interpret::LitToConstInput;
use rustc_middle::ty::print::PrintPolyTraitRefExt as _;
Expand Down Expand Up @@ -200,6 +200,10 @@ pub trait HirTyLowerer<'tcx> {
{
self
}

/// Performs minimalistic dyn compat checks outside of bodies, but full within bodies.
/// Outside of bodies we could end up in cycles, so we delay most checks to later phases.
fn dyn_compatibility_violations(&self, trait_def_id: DefId) -> Vec<DynCompatibilityViolation>;
}

/// The "qualified self" of an associated item path.
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::{self as hir, HirId, ItemLocalMap};
use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
use rustc_infer::infer;
use rustc_infer::traits::Obligation;
use rustc_infer::traits::{DynCompatibilityViolation, Obligation};
use rustc_middle::ty::{self, Const, Ty, TyCtxt, TypeVisitableExt};
use rustc_session::Session;
use rustc_span::{self, DUMMY_SP, ErrorGuaranteed, Ident, Span, sym};
Expand Down Expand Up @@ -388,6 +388,10 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
};
(input_tys, output_ty)
}

fn dyn_compatibility_violations(&self, trait_def_id: DefId) -> Vec<DynCompatibilityViolation> {
self.tcx.dyn_compatibility_violations(trait_def_id).to_vec()
}
}

/// The `ty` representation of a user-provided type. Depending on the use-site
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/async-await/dyn/mut-is-pointer-like.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ LL | #![feature(async_fn_in_dyn_trait)]
= note: `#[warn(incomplete_features)]` on by default

error[E0038]: the trait `AsyncTrait` is not dyn compatible
--> $DIR/mut-is-pointer-like.rs:35:16
--> $DIR/mut-is-pointer-like.rs:35:29
|
LL | let x: Pin<&mut dyn AsyncTrait<Output = ()>> = f;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `AsyncTrait` is not dyn compatible
| ^^^^^^^^^^^^^^^^^^^^^^^ `AsyncTrait` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/async-await/dyn/works.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ LL | #![feature(async_fn_in_dyn_trait)]
= note: `#[warn(incomplete_features)]` on by default

error[E0038]: the trait `AsyncTrait` is not dyn compatible
--> $DIR/works.rs:27:16
--> $DIR/works.rs:27:21
|
LL | let x: &dyn AsyncTrait = &"hello, world!";
| ^^^^^^^^^^^^^^^ `AsyncTrait` is not dyn compatible
| ^^^^^^^^^^ `AsyncTrait` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/async-await/dyn/wrong-size.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ LL | #![feature(async_fn_in_dyn_trait)]
= note: `#[warn(incomplete_features)]` on by default

error[E0038]: the trait `AsyncTrait` is not dyn compatible
--> $DIR/wrong-size.rs:21:12
--> $DIR/wrong-size.rs:21:17
|
LL | let x: &dyn AsyncTrait = &"hello, world!";
| ^^^^^^^^^^^^^^^ `AsyncTrait` is not dyn compatible
| ^^^^^^^^^^ `AsyncTrait` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/async-await/in-trait/dyn-compatibility.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0038]: the trait `Foo` is not dyn compatible
--> $DIR/dyn-compatibility.rs:9:12
--> $DIR/dyn-compatibility.rs:9:17
|
LL | let x: &dyn Foo = todo!();
| ^^^^^^^^ `Foo` is not dyn compatible
| ^^^ `Foo` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ fn main() {
let _: &Copy + 'static; //~ ERROR expected a path
//~^ ERROR is not dyn compatible
let _: &'static Copy + 'static; //~ ERROR expected a path
//~^ ERROR is not dyn compatible
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@ LL | let _: &'static (Copy + 'static);
| + +

error[E0038]: the trait `Copy` is not dyn compatible
--> $DIR/trait-object-reference-without-parens-suggestion.rs:4:12
--> $DIR/trait-object-reference-without-parens-suggestion.rs:4:13
|
LL | let _: &Copy + 'static;
| ^^^^^ `Copy` is not dyn compatible
| ^^^^ `Copy` is not dyn compatible
|
= note: the trait is not dyn compatible because it requires `Self: Sized`
= note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>

error[E0038]: the trait `Copy` is not dyn compatible
--> $DIR/trait-object-reference-without-parens-suggestion.rs:6:21
|
LL | let _: &'static Copy + 'static;
| ^^^^ `Copy` is not dyn compatible
|
= note: the trait is not dyn compatible because it requires `Self: Sized`
= note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0038, E0178.
For more information about an error, try `rustc --explain E0038`.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ LL | fn transmute(&self, t: T) -> <Self as Super<NotActuallySuper>>::Assoc;
= help: consider moving `transmute` to another trait

error[E0038]: the trait `Foo` is not dyn compatible
--> $DIR/almost-supertrait-associated-type.rs:7:27
--> $DIR/almost-supertrait-associated-type.rs:7:32
|
LL | (&PhantomData::<T> as &dyn Foo<T, U>).transmute(t)
| ^^^^^^^^^^^^^^ `Foo` is not dyn compatible
| ^^^^^^^^^ `Foo` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/dyn-compatibility/generics.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ LL | fn bar<T>(&self, t: T);
= help: consider moving `bar` to another trait

error[E0038]: the trait `Bar` is not dyn compatible
--> $DIR/generics.rs:22:10
--> $DIR/generics.rs:22:15
|
LL | t as &dyn Bar
| ^^^^^^^^ `Bar` is not dyn compatible
| ^^^ `Bar` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0038]: the trait `Bar` is not dyn compatible
--> $DIR/mention-correct-dyn-incompatible-trait.rs:19:15
--> $DIR/mention-correct-dyn-incompatible-trait.rs:19:24
|
LL | let test: &mut dyn Bar = &mut thing;
| ^^^^^^^^^^^^ `Bar` is not dyn compatible
| ^^^ `Bar` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/dyn-compatibility/no-static.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ LL | fn foo() where Self: Sized {}
| +++++++++++++++++

error[E0038]: the trait `Foo` is not dyn compatible
--> $DIR/no-static.rs:18:12
--> $DIR/no-static.rs:18:20
|
LL | let b: Box<dyn Foo> = Box::new(Bar);
| ^^^^^^^^^^^^ `Foo` is not dyn compatible
| ^^^ `Foo` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0038]: the trait `Trait` is not dyn compatible
--> $DIR/feature-gate-dispatch-from-dyn-missing-impl.rs:32:25
--> $DIR/feature-gate-dispatch-from-dyn-missing-impl.rs:32:33
|
LL | fn ptr(self: Ptr<Self>);
| --------- help: consider changing method `ptr`'s `self` parameter to be `&self`: `&Self`
...
LL | Ptr(Box::new(4)) as Ptr<dyn Trait>;
| ^^^^^^^^^^^^^^ `Trait` is not dyn compatible
| ^^^^^ `Trait` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/generic-associated-types/issue-76535.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ LL | let sub: Box<dyn SuperTrait<SubType<'a> = SubStruct>> = Box::new(SuperS
| ++++

error[E0038]: the trait `SuperTrait` is not dyn compatible
--> $DIR/issue-76535.rs:34:14
--> $DIR/issue-76535.rs:34:22
|
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` is not dyn compatible
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/generic-associated-types/issue-78671.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ LL | Box::new(Family) as &dyn CollectionFamily<Member<T>=usize>
| +++

error[E0038]: the trait `CollectionFamily` is not dyn compatible
--> $DIR/issue-78671.rs:5:25
--> $DIR/issue-78671.rs:5:30
|
LL | Box::new(Family) as &dyn CollectionFamily<Member=usize>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `CollectionFamily` is not dyn compatible
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `CollectionFamily` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/generic-associated-types/issue-79422.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ LL | as Box<dyn MapLike<u8, u8, VRefCont<'a> = dyn RefCont<'_, u8>>>;
| ++++

error[E0038]: the trait `MapLike` is not dyn compatible
--> $DIR/issue-79422.rs:45:12
--> $DIR/issue-79422.rs:45:20
|
LL | as Box<dyn MapLike<u8, u8, VRefCont = dyn RefCont<'_, u8>>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `MapLike` is not dyn compatible
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `MapLike` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ fn main() {
//~^ ERROR the trait `Foo` is not dyn compatible

needs_bar(x);
//~^ ERROR mismatched types
}
25 changes: 4 additions & 21 deletions tests/ui/higher-ranked/trait-bounds/span-bug-issue-121597.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0038]: the trait `Foo` is not dyn compatible
--> $DIR/span-bug-issue-121597.rs:14:12
--> $DIR/span-bug-issue-121597.rs:14:17
|
LL | let x: &dyn Foo = &();
| ^^^^^^^^ `Foo` is not dyn compatible
| ^^^ `Foo` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
Expand All @@ -13,23 +13,6 @@ LL | trait Foo: for<T> Bar<T> {}
| |
| this trait is not dyn compatible...

error[E0308]: mismatched types
--> $DIR/span-bug-issue-121597.rs:17:15
|
LL | needs_bar(x);
| --------- ^ types differ in mutability
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*mut Type2`
found reference `&dyn Foo`
note: function defined here
--> $DIR/span-bug-issue-121597.rs:11:4
|
LL | fn needs_bar(_: *mut Type2) {}
| ^^^^^^^^^ -------------

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0038, E0308.
For more information about an error, try `rustc --explain E0038`.
For more information about this error, try `rustc --explain E0038`.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ impl DynIncompatible for B {

fn car() -> dyn DynIncompatible { //~ ERROR the trait `DynIncompatible` is not dyn compatible
//~^ ERROR return type cannot be a trait object without pointer indirection
//~| ERROR the trait `DynIncompatible` is not dyn compatible
//~| ERROR the trait `DynIncompatible` is not dyn compatible
if true {
return A;
}
Expand Down
Loading
Loading