@@ -491,7 +491,33 @@ fn ty_is_local_constructor(ty: Ty<'_>, in_crate: InCrate) -> bool {
491
491
ty:: Never |
492
492
ty:: Tuple ( ..) |
493
493
ty:: Param ( ..) |
494
- ty:: Projection ( ..) => {
494
+ ty:: Projection ( ..) |
495
+ // This merits some explanation.
496
+ // Normally, opaque types are not involed when performing
497
+ // coherence checking, since it is illegal to directly
498
+ // implement a trait on an opaque type. However, we might
499
+ // end up looking at an opaque type during coherence checking
500
+ // if an opaque type gets used within another type (e.g. as
501
+ // a type parameter). This requires us to decide whether or
502
+ // not an opaque type should be considered 'local' or not.
503
+ //
504
+ // We choose to treat all opaque types as non-local, even
505
+ // those that appear within the same crate. This seems
506
+ // somewhat suprising at first, but makes sense when
507
+ // you consider that opaque types are supposed to hide
508
+ // the underlying type *within the same crate*. When an
509
+ // opaque type is used from outside the module
510
+ // where it is declared, it should be impossible to observe
511
+ // anyything about it other than the traits that it implements.
512
+ //
513
+ // The alternative would be to look at the underlying type
514
+ // to determine whether or not the opaque type itself should
515
+ // be considered local. However, this could make it a breaking change
516
+ // to switch the underlying ('defining') type from a local type
517
+ // to a remote type. This would violate the rule that opaque
518
+ // types should be completely opaque apart from the traits
519
+ // that they implement, so we don't use this behavior.
520
+ ty:: Opaque ( ..) => {
495
521
false
496
522
}
497
523
@@ -518,8 +544,7 @@ fn ty_is_local_constructor(ty: Ty<'_>, in_crate: InCrate) -> bool {
518
544
ty:: UnnormalizedProjection ( ..) |
519
545
ty:: Closure ( ..) |
520
546
ty:: Generator ( ..) |
521
- ty:: GeneratorWitness ( ..) |
522
- ty:: Opaque ( ..) => {
547
+ ty:: GeneratorWitness ( ..) => {
523
548
bug ! ( "ty_is_local invoked on unexpected type: {:?}" , ty)
524
549
}
525
550
}
0 commit comments