Skip to content

Commit 7d0a182

Browse files
committed
orphan check: opaque types are an error
1 parent 039a6ad commit 7d0a182

File tree

2 files changed

+2
-30
lines changed

2 files changed

+2
-30
lines changed

compiler/rustc_trait_selection/src/traits/coherence.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -739,34 +739,6 @@ fn ty_is_local_constructor(tcx: TyCtxt<'_>, ty: Ty<'_>, in_crate: InCrate) -> bo
739739

740740
ty::Adt(def, _) => def_id_is_local(def.did(), in_crate),
741741
ty::Foreign(did) => def_id_is_local(did, in_crate),
742-
ty::Opaque(..) => {
743-
// This merits some explanation.
744-
// Normally, opaque types are not involved when performing
745-
// coherence checking, since it is illegal to directly
746-
// implement a trait on an opaque type. However, we might
747-
// end up looking at an opaque type during coherence checking
748-
// if an opaque type gets used within another type (e.g. as
749-
// a type parameter). This requires us to decide whether or
750-
// not an opaque type should be considered 'local' or not.
751-
//
752-
// We choose to treat all opaque types as non-local, even
753-
// those that appear within the same crate. This seems
754-
// somewhat surprising at first, but makes sense when
755-
// you consider that opaque types are supposed to hide
756-
// the underlying type *within the same crate*. When an
757-
// opaque type is used from outside the module
758-
// where it is declared, it should be impossible to observe
759-
// anything about it other than the traits that it implements.
760-
//
761-
// The alternative would be to look at the underlying type
762-
// to determine whether or not the opaque type itself should
763-
// be considered local. However, this could make it a breaking change
764-
// to switch the underlying ('defining') type from a local type
765-
// to a remote type. This would violate the rule that opaque
766-
// types should be completely opaque apart from the traits
767-
// that they implement, so we don't use this behavior.
768-
false
769-
}
770742

771743
ty::Dynamic(ref tt, ..) => {
772744
if let Some(principal) = tt.principal() {
@@ -786,7 +758,7 @@ fn ty_is_local_constructor(tcx: TyCtxt<'_>, ty: Ty<'_>, in_crate: InCrate) -> bo
786758
//
787759
// See `test/ui/coherence/coherence-with-closure.rs` for an example where this
788760
// could happens.
789-
ty::Closure(..) | ty::Generator(..) | ty::GeneratorWitness(..) => {
761+
ty::Opaque(..) | ty::Closure(..) | ty::Generator(..) | ty::GeneratorWitness(..) => {
790762
tcx.sess.delay_span_bug(
791763
DUMMY_SP,
792764
format!("ty_is_local invoked on closure or generator: {:?}", ty),

src/test/ui/impl-trait/negative-reasoning.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | impl<T: std::fmt::Debug> AnotherTrait for T {}
77
LL | impl AnotherTrait for D<OpaqueType> {
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `D<OpaqueType>`
99
|
10-
= note: upstream crates may add a new impl of trait `std::fmt::Debug` for type `OpaqueType` in future versions
10+
= note: downstream crates may implement trait `std::fmt::Debug` for type `OpaqueType`
1111

1212
error: cannot implement trait on type alias impl trait
1313
--> $DIR/negative-reasoning.rs:19:25

0 commit comments

Comments
 (0)