Skip to content

Merge unboxed trait object error suggestion into regular dyn incompat error #142458

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 0 additions & 66 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,6 @@ fn check_trait_item<'tcx>(
_ => (None, trait_item.span),
};

check_dyn_incompatible_self_trait_by_name(tcx, trait_item);

// Check that an item definition in a subtrait is shadowing a supertrait item.
lint_item_shadowing_supertrait_item(tcx, def_id);

Expand Down Expand Up @@ -832,70 +830,6 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GATArgsCollector<'tcx> {
}
}

fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool {
match ty.kind {
hir::TyKind::TraitObject([trait_ref], ..) => match trait_ref.trait_ref.path.segments {
[s] => s.res.opt_def_id() == Some(trait_def_id.to_def_id()),
_ => false,
},
_ => false,
}
}

/// Detect when a dyn-incompatible trait is referring to itself in one of its associated items.
///
/// In such cases, suggest using `Self` instead.
fn check_dyn_incompatible_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem<'_>) {
let (trait_ident, trait_def_id) =
match tcx.hir_node_by_def_id(tcx.hir_get_parent_item(item.hir_id()).def_id) {
hir::Node::Item(item) => match item.kind {
hir::ItemKind::Trait(_, _, ident, ..) => (ident, item.owner_id),
_ => return,
},
_ => return,
};
let mut trait_should_be_self = vec![];
match &item.kind {
hir::TraitItemKind::Const(ty, _) | hir::TraitItemKind::Type(_, Some(ty))
if could_be_self(trait_def_id.def_id, ty) =>
{
trait_should_be_self.push(ty.span)
}
hir::TraitItemKind::Fn(sig, _) => {
for ty in sig.decl.inputs {
if could_be_self(trait_def_id.def_id, ty) {
trait_should_be_self.push(ty.span);
}
}
match sig.decl.output {
hir::FnRetTy::Return(ty) if could_be_self(trait_def_id.def_id, ty) => {
trait_should_be_self.push(ty.span);
}
_ => {}
}
}
_ => {}
}
if !trait_should_be_self.is_empty() {
if tcx.is_dyn_compatible(trait_def_id) {
return;
}
let sugg = trait_should_be_self.iter().map(|span| (*span, "Self".to_string())).collect();
tcx.dcx()
.struct_span_err(
trait_should_be_self,
"associated item referring to unboxed trait object for its own trait",
)
.with_span_label(trait_ident.span, "in this trait")
.with_multipart_suggestion(
"you might have meant to use `Self` to refer to the implementing type",
sugg,
Applicability::MachineApplicable,
)
.emit();
}
}

fn lint_item_shadowing_supertrait_item<'tcx>(tcx: TyCtxt<'tcx>, trait_item_def_id: LocalDefId) {
let item_name = tcx.item_name(trait_item_def_id.to_def_id());
let trait_def_id = tcx.local_parent(trait_item_def_id);
Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_wf_check.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use rustc_hir::def::DefKind;
use rustc_hir::intravisit::{self, Visitor, VisitorExt};
use rustc_hir::{self as hir, AmbigArg, ForeignItem, ForeignItemKind};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::{ObligationCause, WellFormedLoc};
use rustc_infer::traits::{ObligationCause, ObligationCauseCode, WellFormedLoc};
use rustc_middle::bug;
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt, TypingMode, fold_regions};
Expand Down Expand Up @@ -107,6 +108,17 @@ fn diagnostic_hir_wf_check<'tcx>(
// over less-specific types (e.g. `Option<MyStruct<u8>>`)
if self.depth >= self.cause_depth {
self.cause = Some(error.obligation.cause);
if let hir::TyKind::TraitObject(..) = ty.kind {
if let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn =
self.tcx.def_kind(self.def_id)
{
self.cause = Some(ObligationCause::new(
ty.span,
self.def_id,
ObligationCauseCode::DynCompatible(ty.span),
));
}
}
self.cause_depth = self.depth
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ pub enum ObligationCauseCode<'tcx> {

RustCall,

DynCompatible(Span),

/// Obligations to prove that a `Drop` or negative auto trait impl is not stronger than
/// the ADT it's being implemented for.
AlwaysApplicableImpl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2721,6 +2721,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
ObligationCauseCode::TupleElem => {
err.note("only the last element of a tuple may have a dynamically sized type");
}
ObligationCauseCode::DynCompatible(span) => {
err.multipart_suggestion(
"you might have meant to use `Self` to refer to the implementing type",
vec![(span, "Self".into())],
Applicability::MachineApplicable,
);
}
ObligationCauseCode::WhereClause(item_def_id, span)
| ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)
| ObligationCauseCode::HostEffectInExpr(item_def_id, span, ..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::traits::{
///
/// Currently that is `Self` in supertraits. This is needed
/// because `dyn_compatibility_violations` can't be used during
/// type collection.
/// type collection, as type collection is needed for `dyn_compatiblity_violations` itself.
#[instrument(level = "debug", skip(tcx), ret)]
pub fn hir_ty_lowering_dyn_compatibility_violations(
tcx: TyCtxt<'_>,
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/dyn-compatibility/avoid-ice-on-warning-3.old.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ help: alternatively, consider constraining `g` so it does not apply to trait obj
|
LL | trait A { fn g(b: B) -> B where Self: Sized; }
| +++++++++++++++++
help: you might have meant to use `Self` to refer to the implementing type
|
LL - trait B { fn f(a: A) -> A; }
LL + trait B { fn f(a: Self) -> A; }
|

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/avoid-ice-on-warning-3.rs:14:19
Expand Down Expand Up @@ -124,6 +129,11 @@ help: alternatively, consider constraining `f` so it does not apply to trait obj
|
LL | trait B { fn f(a: A) -> A where Self: Sized; }
| +++++++++++++++++
help: you might have meant to use `Self` to refer to the implementing type
|
LL - trait A { fn g(b: B) -> B; }
LL + trait A { fn g(b: Self) -> B; }
|

error: aborting due to 2 previous errors; 6 warnings emitted

Expand Down
3 changes: 1 addition & 2 deletions tests/ui/dyn-compatibility/supertrait-mentions-GAT.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ trait GatTrait {

trait SuperTrait<T>: for<'a> GatTrait<Gat<'a> = T> {
fn c(&self) -> dyn SuperTrait<T>;
//~^ ERROR associated item referring to unboxed trait object for its own trait
//~| ERROR the trait `SuperTrait` is not dyn compatible
//~^ ERROR the trait `SuperTrait` is not dyn compatible
}

fn main() {}
21 changes: 6 additions & 15 deletions tests/ui/dyn-compatibility/supertrait-mentions-GAT.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,6 @@ LL | Self: 'a;
| ^^
= help: consider adding an explicit lifetime bound `Self: 'a`...

error: associated item referring to unboxed trait object for its own trait
--> $DIR/supertrait-mentions-GAT.rs:10:20
|
LL | trait SuperTrait<T>: for<'a> GatTrait<Gat<'a> = T> {
| ---------- in this trait
LL | fn c(&self) -> dyn SuperTrait<T>;
| ^^^^^^^^^^^^^^^^^
|
help: you might have meant to use `Self` to refer to the implementing type
|
LL - fn c(&self) -> dyn SuperTrait<T>;
LL + fn c(&self) -> Self;
|

error[E0038]: the trait `SuperTrait` is not dyn compatible
--> $DIR/supertrait-mentions-GAT.rs:10:20
|
Expand All @@ -37,8 +23,13 @@ LL | type Gat<'a>
LL | trait SuperTrait<T>: for<'a> GatTrait<Gat<'a> = T> {
| ---------- this trait is not dyn compatible...
= help: consider moving `Gat` to another trait
help: you might have meant to use `Self` to refer to the implementing type
|
LL - fn c(&self) -> dyn SuperTrait<T>;
LL + fn c(&self) -> Self;
|

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

Some errors have detailed explanations: E0038, E0311.
For more information about an error, try `rustc --explain E0038`.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ trait A: Sized {
fn f(a: A) -> A;
//~^ ERROR expected a type, found a trait
//~| ERROR expected a type, found a trait
//~| ERROR associated item referring to unboxed trait object for its own trait
}
trait B {
fn f(b: B) -> B;
//~^ ERROR expected a type, found a trait
//~| ERROR expected a type, found a trait
//~| ERROR associated item referring to unboxed trait object for its own trait
}
trait C {
fn f(&self, c: C) -> C;
//~^ ERROR expected a type, found a trait
//~| ERROR expected a type, found a trait
//~| ERROR associated item referring to unboxed trait object for its own trait
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,8 @@ help: `A` is dyn-incompatible, use `impl A` to return an opaque type, as long as
LL | fn f(a: A) -> impl A;
| ++++

error: associated item referring to unboxed trait object for its own trait
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:4:13
|
LL | trait A: Sized {
| - in this trait
LL | fn f(a: A) -> A;
| ^ ^
|
help: you might have meant to use `Self` to refer to the implementing type
|
LL - fn f(a: A) -> A;
LL + fn f(a: Self) -> Self;
|

error[E0782]: expected a type, found a trait
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:10:13
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:9:13
|
LL | fn f(b: B) -> B;
| ^
Expand All @@ -58,7 +44,7 @@ LL | fn f(b: impl B) -> B;
| ++++

error[E0782]: expected a type, found a trait
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:10:19
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:9:19
|
LL | fn f(b: B) -> B;
| ^
Expand All @@ -68,22 +54,8 @@ help: `B` is dyn-incompatible, use `impl B` to return an opaque type, as long as
LL | fn f(b: B) -> impl B;
| ++++

error: associated item referring to unboxed trait object for its own trait
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:10:13
|
LL | trait B {
| - in this trait
LL | fn f(b: B) -> B;
| ^ ^
|
help: you might have meant to use `Self` to refer to the implementing type
|
LL - fn f(b: B) -> B;
LL + fn f(b: Self) -> Self;
|

error[E0782]: expected a type, found a trait
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:16:20
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:14:20
|
LL | fn f(&self, c: C) -> C;
| ^
Expand All @@ -100,7 +72,7 @@ LL | fn f(&self, c: impl C) -> C;
| ++++

error[E0782]: expected a type, found a trait
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:16:26
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:14:26
|
LL | fn f(&self, c: C) -> C;
| ^
Expand All @@ -110,20 +82,6 @@ help: `C` is dyn-incompatible, use `impl C` to return an opaque type, as long as
LL | fn f(&self, c: C) -> impl C;
| ++++

error: associated item referring to unboxed trait object for its own trait
--> $DIR/dyn-incompatible-trait-should-use-self-2021-without-dyn.rs:16:20
|
LL | trait C {
| - in this trait
LL | fn f(&self, c: C) -> C;
| ^ ^
|
help: you might have meant to use `Self` to refer to the implementing type
|
LL - fn f(&self, c: C) -> C;
LL + fn f(&self, c: Self) -> Self;
|

error: aborting due to 9 previous errors
error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0782`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
#![allow(bare_trait_objects)]
trait A: Sized {
fn f(a: dyn A) -> dyn A;
//~^ ERROR associated item referring to unboxed trait object for its own trait
//~| ERROR the trait `A` is not dyn compatible
//~^ ERROR the trait `A` is not dyn compatible
}
trait B {
fn f(a: dyn B) -> dyn B;
//~^ ERROR associated item referring to unboxed trait object for its own trait
//~| ERROR the trait `B` is not dyn compatible
//~^ ERROR the trait `B` is not dyn compatible
}
trait C {
fn f(&self, a: dyn C) -> dyn C;
Expand Down
Loading
Loading