Skip to content

Commit

Permalink
Rollup merge of rust-lang#56862 - arielb1:fundamentally-clean, r=niko…
Browse files Browse the repository at this point in the history
…matsakis

stop treating trait objects from #[fundamental] traits as fundamental

This is a [breaking-change] to code that exploits this functionality (which should be limited to code using `#![feature(fundamental)]`.

Fixes rust-lang#56503.

r? @nikomatsakis
  • Loading branch information
Centril committed Dec 22, 2018
2 parents e0fb972 + c4fa1d2 commit 06b0f46
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/librustc/traits/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ fn uncovered_tys<'tcx>(tcx: TyCtxt<'_, '_, '_>, ty: Ty<'tcx>, in_crate: InCrate)
-> Vec<Ty<'tcx>> {
if ty_is_local_constructor(ty, in_crate) {
vec![]
} else if fundamental_ty(tcx, ty) {
} else if fundamental_ty(ty) {
ty.walk_shallow()
.flat_map(|t| uncovered_tys(tcx, t, in_crate))
.collect()
Expand All @@ -415,14 +415,13 @@ fn is_possibly_remote_type(ty: Ty<'_>, _in_crate: InCrate) -> bool {

fn ty_is_local(tcx: TyCtxt<'_, '_, '_>, ty: Ty<'_>, in_crate: InCrate) -> bool {
ty_is_local_constructor(ty, in_crate) ||
fundamental_ty(tcx, ty) && ty.walk_shallow().any(|t| ty_is_local(tcx, t, in_crate))
fundamental_ty(ty) && ty.walk_shallow().any(|t| ty_is_local(tcx, t, in_crate))
}

fn fundamental_ty(tcx: TyCtxt<'_, '_, '_>, ty: Ty<'_>) -> bool {
fn fundamental_ty(ty: Ty<'_>) -> bool {
match ty.sty {
ty::Ref(..) => true,
ty::Adt(def, _) => def.is_fundamental(),
ty::Dynamic(ref data, ..) => tcx.has_attr(data.principal().def_id(), "fundamental"),
_ => false
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![crate_type = "rlib"]
#![feature(fundamental)]

pub trait Misc {}

#[fundamental]
pub trait Fundamental<T> {}
15 changes: 15 additions & 0 deletions src/test/ui/coherence/coherence-fundamental-trait-objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Check that trait objects from #[fundamental] traits are not
// treated as #[fundamental] types - the 2 meanings of #[fundamental]
// are distinct.

// aux-build:coherence_fundamental_trait_lib.rs

extern crate coherence_fundamental_trait_lib;

use coherence_fundamental_trait_lib::{Fundamental, Misc};

pub struct Local;
impl Misc for dyn Fundamental<Local> {}
//~^ ERROR E0117

fn main() {}
12 changes: 12 additions & 0 deletions src/test/ui/coherence/coherence-fundamental-trait-objects.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> $DIR/coherence-fundamental-trait-objects.rs:12:1
|
LL | impl Misc for dyn Fundamental<Local> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use types inside crate
|
= note: the impl does not reference any types defined in this crate
= note: define and implement a trait or new type instead

error: aborting due to previous error

For more information about this error, try `rustc --explain E0117`.

0 comments on commit 06b0f46

Please sign in to comment.