Skip to content

Commit

Permalink
Don't consider regions in deref_into_dyn_supertrait lint
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Nov 17, 2023
1 parent 069a4af commit bea815b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
16 changes: 11 additions & 5 deletions compiler/rustc_lint/src/deref_into_dyn_supertrait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ declare_lint_pass!(DerefIntoDynSupertrait => [DEREF_INTO_DYN_SUPERTRAIT]);

impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
let tcx = cx.tcx;
// `Deref` is being implemented for `t`
if let hir::ItemKind::Impl(impl_) = item.kind
&& let Some(trait_) = &impl_.of_trait
&& let t = cx.tcx.type_of(item.owner_id).instantiate_identity()
&& let t = tcx.type_of(item.owner_id).instantiate_identity()
&& let opt_did @ Some(did) = trait_.trait_def_id()
&& opt_did == cx.tcx.lang_items().deref_trait()
&& opt_did == tcx.lang_items().deref_trait()
// `t` is `dyn t_principal`
&& let ty::Dynamic(data, _, ty::Dyn) = t.kind()
&& let Some(t_principal) = data.principal()
Expand All @@ -73,17 +74,22 @@ impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
&& let ty::Dynamic(data, _, ty::Dyn) = target.kind()
&& let Some(target_principal) = data.principal()
// `target_principal` is a supertrait of `t_principal`
&& supertraits(cx.tcx, t_principal.with_self_ty(cx.tcx, cx.tcx.types.trait_object_dummy_self))
.any(|sup| sup.map_bound(|x| ty::ExistentialTraitRef::erase_self_ty(cx.tcx, x)) == target_principal)
&& supertraits(tcx, t_principal.with_self_ty(tcx, tcx.types.trait_object_dummy_self))
.any(|sup| {
tcx.erase_regions(
sup.map_bound(|x| ty::ExistentialTraitRef::erase_self_ty(tcx, x)),
) == tcx.erase_regions(target_principal)
})
{
let t = tcx.erase_regions(t);
let label = impl_
.items
.iter()
.find_map(|i| (i.ident.name == sym::Target).then_some(i.span))
.map(|label| SupertraitAsDerefTargetLabel { label });
cx.emit_spanned_lint(
DEREF_INTO_DYN_SUPERTRAIT,
cx.tcx.def_span(item.owner_id.def_id),
tcx.def_span(item.owner_id.def_id),
SupertraitAsDerefTarget { t, target_principal, label },
);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/traits/trait-upcasting/migrate-lint-deny-regions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![deny(deref_into_dyn_supertrait)]

use std::ops::Deref;

trait Bar<'a> {}
trait Foo<'a>: Bar<'a> {}

impl<'a> Deref for dyn Foo<'a> {
//~^ ERROR dyn Foo<'_>` implements `Deref` with supertrait `Bar<'_>` as target
//~| WARN this was previously accepted by the compiler
type Target = dyn Bar<'a>;

fn deref(&self) -> &Self::Target {
todo!()
}
}

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/traits/trait-upcasting/migrate-lint-deny-regions.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: `dyn Foo<'_>` implements `Deref` with supertrait `Bar<'_>` as target
--> $DIR/migrate-lint-deny-regions.rs:8:1
|
LL | impl<'a> Deref for dyn Foo<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | type Target = dyn Bar<'a>;
| -------------------------- target type is set here
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #89460 <https://github.com/rust-lang/rust/issues/89460>
note: the lint level is defined here
--> $DIR/migrate-lint-deny-regions.rs:1:9
|
LL | #![deny(deref_into_dyn_supertrait)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

6 changes: 2 additions & 4 deletions tests/ui/traits/trait-upcasting/migrate-lint-deny.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#![deny(deref_into_dyn_supertrait)]

extern crate core;

use core::ops::Deref;
use std::ops::Deref;

// issue 89190
trait A {}
trait B: A {}

impl<'a> Deref for dyn 'a + B {
//~^ ERROR `(dyn B + 'a)` implements `Deref` with supertrait `A` as target
//~^ ERROR `dyn B` implements `Deref` with supertrait `A` as target
//~| WARN this was previously accepted by the compiler but is being phased out;

type Target = dyn A;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/trait-upcasting/migrate-lint-deny.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `(dyn B + 'a)` implements `Deref` with supertrait `A` as target
--> $DIR/migrate-lint-deny.rs:11:1
error: `dyn B` implements `Deref` with supertrait `A` as target
--> $DIR/migrate-lint-deny.rs:9:1
|
LL | impl<'a> Deref for dyn 'a + B {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit bea815b

Please sign in to comment.