Skip to content
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

Revert #126024 "Do not use global caches if opaque types can be defined" #132075

Closed
wants to merge 3 commits into from
Closed
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
33 changes: 19 additions & 14 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1492,9 +1492,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// it's not worth going to more trouble to increase the
// hit-rate, I don't think.
TypingMode::Coherence => false,
// Avoid using the global cache when we're defining opaque types
// as their hidden type may impact the result of candidate selection.
TypingMode::Analysis { defining_opaque_types } => defining_opaque_types.is_empty(),
// FIXME(new-solver): This is incorrect, we should not be using the
// global cache when we're defining opaque types, as their hidden
// type may impact the result of candidate selection. This is still
// a theoretical possibility, however, and we have no example of
// actual unsoundness it can trigger. Until then, not using the
// cache is too big of a compile-time regression.
TypingMode::Analysis { .. } => true,
// The global cache is only used if there are no opaque types in
// the defining scope or we're outside of analysis.
//
Expand Down Expand Up @@ -2358,17 +2362,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
}

ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
if self.infcx.can_define_opaque_ty(def_id) {
unreachable!()
} else {
// We can resolve the `impl Trait` to its concrete type,
// which enforces a DAG between the functions requiring
// the auto trait bounds in question.
match self.tcx().type_of_opaque(def_id) {
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
Err(_) => {
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
}
// FIXME(new-solver): because we're still using the global cache
// when defining opaque types, we can reach here, but shouldn't.
// The test `type-alias-impl-trait/reveal_local.rs` is an
// example.

// We can resolve the `impl Trait` to its concrete type,
// which enforces a DAG between the functions requiring
// the auto trait bounds in question.
match self.tcx().type_of_opaque(def_id) {
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
Err(_) => {
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const-promoted-opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const BAR: () = {
};

const BAZ: &Foo = &FOO;
//[atomic]~^ ERROR: constants cannot refer to interior mutable data
//[string,atomic]~^ ERROR: constants cannot refer to interior mutable data

fn main() {
let _: &'static _ = &FOO;
Expand Down
12 changes: 9 additions & 3 deletions tests/ui/consts/const-promoted-opaque.string.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ LL |
LL | };
| - value is dropped here

error[E0492]: constants cannot refer to interior mutable data
--> $DIR/const-promoted-opaque.rs:32:19
|
LL | const BAZ: &Foo = &FOO;
| ^^^^ this borrow of an interior mutable value may end up in the final value

error[E0716]: temporary value dropped while borrowed
--> $DIR/const-promoted-opaque.rs:36:26
|
Expand All @@ -18,7 +24,7 @@ LL |
LL | }
| - temporary value is freed at the end of this statement

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

Some errors have detailed explanations: E0493, E0716.
For more information about an error, try `rustc --explain E0493`.
Some errors have detailed explanations: E0492, E0493, E0716.
For more information about an error, try `rustc --explain E0492`.
2 changes: 1 addition & 1 deletion tests/ui/type-alias-impl-trait/reveal_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn not_gooder() -> Foo {
// while we could know this from the hidden type, it would
// need extra roundabout logic to support it.
is_send::<Foo>();
//~^ ERROR: type annotations needed: cannot satisfy `Foo: Send`
//~^ ERROR: cannot check whether the hidden type of `reveal_local[9507]::Foo::{opaque#0}` satisfies auto traits

x
}
Expand Down
10 changes: 7 additions & 3 deletions tests/ui/type-alias-impl-trait/reveal_local.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ note: required by a bound in `is_send`
LL | fn is_send<T: Send>() {}
| ^^^^ required by this bound in `is_send`

error[E0283]: type annotations needed: cannot satisfy `Foo: Send`
error: cannot check whether the hidden type of `reveal_local[9507]::Foo::{opaque#0}` satisfies auto traits
--> $DIR/reveal_local.rs:22:15
|
LL | is_send::<Foo>();
| ^^^
|
= note: cannot satisfy `Foo: Send`
= note: fetching the hidden types of an opaque inside of the defining scope is not supported. You can try moving the opaque type and the item that actually registers a hidden type into a new submodule
note: opaque type is declared here
--> $DIR/reveal_local.rs:5:12
|
LL | type Foo = impl Debug;
| ^^^^^^^^^^
note: required by a bound in `is_send`
--> $DIR/reveal_local.rs:7:15
|
Expand All @@ -31,4 +36,3 @@ LL | fn is_send<T: Send>() {}

error: aborting due to 2 previous errors

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