Skip to content

Commit 96b109b

Browse files
committed
Do not try to reveal hidden types when trying to prove Freeze in the defining scope
1 parent 7ddeae9 commit 96b109b

18 files changed

+68
-261
lines changed

compiler/rustc_const_eval/src/check_consts/qualifs.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,33 @@ impl Qualif for HasMutInterior {
100100
}
101101

102102
fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
103-
!ty.is_freeze(cx.tcx, cx.param_env)
103+
// Avoid selecting for simple cases, such as builtin types.
104+
if ty.is_trivially_freeze() {
105+
return false;
106+
}
107+
108+
// We do not use `ty.is_freeze` here, because that requires revealing opaque types, which
109+
// requires borrowck, which in turn will invoke mir_const_qualifs again, causing a cycle error.
110+
// Instead we invoke an obligation context manually, and provide the opaque type inference settings
111+
// that allow the trait solver to just error out instead of cycling.
112+
let freeze_def_id = cx.tcx.require_lang_item(LangItem::Freeze, Some(cx.body.span));
113+
114+
let obligation = Obligation::new(
115+
cx.tcx,
116+
ObligationCause::dummy_with_span(cx.body.span),
117+
cx.param_env,
118+
ty::TraitRef::new(cx.tcx, freeze_def_id, [ty::GenericArg::from(ty)]),
119+
);
120+
121+
let infcx = cx
122+
.tcx
123+
.infer_ctxt()
124+
.with_opaque_type_inference(cx.body.source.def_id().expect_local())
125+
.build();
126+
let ocx = ObligationCtxt::new(&infcx);
127+
ocx.register_obligation(obligation);
128+
let errors = ocx.select_all_or_error();
129+
!errors.is_empty()
104130
}
105131

106132
fn in_adt_inherently<'tcx>(

compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ impl<'tcx> Ty<'tcx> {
12681268
///
12691269
/// Returning true means the type is known to be `Freeze`. Returning
12701270
/// `false` means nothing -- could be `Freeze`, might not be.
1271-
fn is_trivially_freeze(self) -> bool {
1271+
pub fn is_trivially_freeze(self) -> bool {
12721272
match self.kind() {
12731273
ty::Int(_)
12741274
| ty::Uint(_)

compiler/rustc_trait_selection/src/traits/select/mod.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -2385,13 +2385,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23852385
}
23862386

23872387
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
2388-
// We can resolve the `impl Trait` to its concrete type,
2389-
// which enforces a DAG between the functions requiring
2390-
// the auto trait bounds in question.
2391-
match self.tcx().type_of_opaque(def_id) {
2392-
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
2393-
Err(_) => {
2394-
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
2388+
if self.infcx.can_define_opaque_ty(def_id) {
2389+
// We cannot possibly resolve this opaque type, because we are currently computing its hidden type.
2390+
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
2391+
} else {
2392+
// We can resolve the `impl Trait` to its concrete type,
2393+
// which enforces a DAG between the functions requiring
2394+
// the auto trait bounds in question.
2395+
match self.tcx().type_of_opaque(def_id) {
2396+
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
2397+
Err(_) => {
2398+
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
2399+
}
23952400
}
23962401
}
23972402
}

tests/ui/const-generics/opaque_types.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ note: ...which requires const checking `main::{constant#0}`...
122122
|
123123
LL | foo::<42>();
124124
| ^^
125-
= note: ...which requires computing whether `Foo` is freeze...
126-
= note: ...which requires evaluating trait selection obligation `Foo: core::marker::Freeze`...
127125
= note: ...which again requires computing type of opaque `Foo::{opaque#0}`, completing the cycle
128126
note: cycle used when computing type of `Foo::{opaque#0}`
129127
--> $DIR/opaque_types.rs:3:12

tests/ui/consts/const-fn-cycle.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
/// to end up revealing opaque types (the RPIT in `many`'s return type),
88
/// which can quickly lead to cycles.
99
10+
//@ check-pass
11+
1012
pub struct Parser<H>(H);
1113

1214
impl<H, T> Parser<H>
@@ -18,7 +20,6 @@ where
1820
}
1921

2022
pub const fn many<'s>(&'s self) -> Parser<impl for<'a> Fn(&'a str) -> Vec<T> + 's> {
21-
//~^ ERROR: cycle detected
2223
Parser::new(|_| unimplemented!())
2324
}
2425
}

tests/ui/consts/const-fn-cycle.stderr

-34
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
2-
--> $DIR/const-promoted-opaque.rs:29:25
2+
--> $DIR/const-promoted-opaque.rs:28:25
33
|
44
LL | let _: &'static _ = &FOO;
55
| ^^^^
@@ -9,7 +9,7 @@ LL | let _: &'static _ = &FOO;
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

1111
error[E0493]: destructor of `helper::Foo` cannot be evaluated at compile-time
12-
--> $DIR/const-promoted-opaque.rs:29:26
12+
--> $DIR/const-promoted-opaque.rs:28:26
1313
|
1414
LL | let _: &'static _ = &FOO;
1515
| ^^^ the destructor for this type cannot be evaluated in constants
@@ -18,13 +18,13 @@ LL | };
1818
| - value is dropped here
1919

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

2626
error[E0716]: temporary value dropped while borrowed
27-
--> $DIR/const-promoted-opaque.rs:38:26
27+
--> $DIR/const-promoted-opaque.rs:37:26
2828
|
2929
LL | let _: &'static _ = &FOO;
3030
| ---------- ^^^ creates a temporary value which is freed while still in use
@@ -34,38 +34,7 @@ LL |
3434
LL | }
3535
| - temporary value is freed at the end of this statement
3636

37-
error[E0391]: cycle detected when computing type of opaque `helper::Foo::{opaque#0}`
38-
--> $DIR/const-promoted-opaque.rs:14:20
39-
|
40-
LL | pub type Foo = impl Sized;
41-
| ^^^^^^^^^^
42-
|
43-
note: ...which requires borrow-checking `helper::FOO`...
44-
--> $DIR/const-promoted-opaque.rs:21:5
45-
|
46-
LL | pub const FOO: Foo = std::sync::atomic::AtomicU8::new(42);
47-
| ^^^^^^^^^^^^^^^^^^
48-
note: ...which requires promoting constants in MIR for `helper::FOO`...
49-
--> $DIR/const-promoted-opaque.rs:21:5
50-
|
51-
LL | pub const FOO: Foo = std::sync::atomic::AtomicU8::new(42);
52-
| ^^^^^^^^^^^^^^^^^^
53-
note: ...which requires const checking `helper::FOO`...
54-
--> $DIR/const-promoted-opaque.rs:21:5
55-
|
56-
LL | pub const FOO: Foo = std::sync::atomic::AtomicU8::new(42);
57-
| ^^^^^^^^^^^^^^^^^^
58-
= note: ...which requires computing whether `helper::Foo` is freeze...
59-
= note: ...which requires evaluating trait selection obligation `helper::Foo: core::marker::Freeze`...
60-
= note: ...which again requires computing type of opaque `helper::Foo::{opaque#0}`, completing the cycle
61-
note: cycle used when computing type of `helper::Foo::{opaque#0}`
62-
--> $DIR/const-promoted-opaque.rs:14:20
63-
|
64-
LL | pub type Foo = impl Sized;
65-
| ^^^^^^^^^^
66-
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
67-
68-
error: aborting due to 5 previous errors
37+
error: aborting due to 4 previous errors
6938

70-
Some errors have detailed explanations: E0391, E0492, E0493, E0658, E0716.
71-
For more information about an error, try `rustc --explain E0391`.
39+
Some errors have detailed explanations: E0492, E0493, E0658, E0716.
40+
For more information about an error, try `rustc --explain E0492`.

tests/ui/consts/const-promoted-opaque.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
mod helper {
1414
pub type Foo = impl Sized;
15-
//[string,atomic]~^ ERROR cycle detected
1615

1716
#[cfg(string)]
1817
pub const FOO: Foo = String::new();
@@ -28,11 +27,11 @@ use helper::*;
2827
const BAR: () = {
2928
let _: &'static _ = &FOO;
3029
//[string,atomic]~^ ERROR: destructor of `helper::Foo` cannot be evaluated at compile-time
31-
//[string,atomic]~| ERROR: cannot borrow here
30+
//[atomic]~| ERROR: cannot borrow here
3231
};
3332

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

3736
fn main() {
3837
let _: &'static _ = &FOO;
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
1-
error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
2-
--> $DIR/const-promoted-opaque.rs:29:25
3-
|
4-
LL | let _: &'static _ = &FOO;
5-
| ^^^^
6-
|
7-
= note: see issue #80384 <https://github.com/rust-lang/rust/issues/80384> for more information
8-
= help: add `#![feature(const_refs_to_cell)]` to the crate attributes to enable
9-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10-
111
error[E0493]: destructor of `helper::Foo` cannot be evaluated at compile-time
12-
--> $DIR/const-promoted-opaque.rs:29:26
2+
--> $DIR/const-promoted-opaque.rs:28:26
133
|
144
LL | let _: &'static _ = &FOO;
155
| ^^^ the destructor for this type cannot be evaluated in constants
166
...
177
LL | };
188
| - value is dropped here
199

20-
error[E0492]: constants cannot refer to interior mutable data
21-
--> $DIR/const-promoted-opaque.rs:34:19
22-
|
23-
LL | const BAZ: &Foo = &FOO;
24-
| ^^^^ this borrow of an interior mutable value may end up in the final value
25-
2610
error[E0716]: temporary value dropped while borrowed
27-
--> $DIR/const-promoted-opaque.rs:38:26
11+
--> $DIR/const-promoted-opaque.rs:37:26
2812
|
2913
LL | let _: &'static _ = &FOO;
3014
| ---------- ^^^ creates a temporary value which is freed while still in use
@@ -34,38 +18,7 @@ LL |
3418
LL | }
3519
| - temporary value is freed at the end of this statement
3620

37-
error[E0391]: cycle detected when computing type of opaque `helper::Foo::{opaque#0}`
38-
--> $DIR/const-promoted-opaque.rs:14:20
39-
|
40-
LL | pub type Foo = impl Sized;
41-
| ^^^^^^^^^^
42-
|
43-
note: ...which requires borrow-checking `helper::FOO`...
44-
--> $DIR/const-promoted-opaque.rs:18:5
45-
|
46-
LL | pub const FOO: Foo = String::new();
47-
| ^^^^^^^^^^^^^^^^^^
48-
note: ...which requires promoting constants in MIR for `helper::FOO`...
49-
--> $DIR/const-promoted-opaque.rs:18:5
50-
|
51-
LL | pub const FOO: Foo = String::new();
52-
| ^^^^^^^^^^^^^^^^^^
53-
note: ...which requires const checking `helper::FOO`...
54-
--> $DIR/const-promoted-opaque.rs:18:5
55-
|
56-
LL | pub const FOO: Foo = String::new();
57-
| ^^^^^^^^^^^^^^^^^^
58-
= note: ...which requires computing whether `helper::Foo` is freeze...
59-
= note: ...which requires evaluating trait selection obligation `helper::Foo: core::marker::Freeze`...
60-
= note: ...which again requires computing type of opaque `helper::Foo::{opaque#0}`, completing the cycle
61-
note: cycle used when computing type of `helper::Foo::{opaque#0}`
62-
--> $DIR/const-promoted-opaque.rs:14:20
63-
|
64-
LL | pub type Foo = impl Sized;
65-
| ^^^^^^^^^^
66-
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
67-
68-
error: aborting due to 5 previous errors
21+
error: aborting due to 2 previous errors
6922

70-
Some errors have detailed explanations: E0391, E0492, E0493, E0658, E0716.
71-
For more information about an error, try `rustc --explain E0391`.
23+
Some errors have detailed explanations: E0493, E0716.
24+
For more information about an error, try `rustc --explain E0493`.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0599]: no method named `my_debug` found for opaque type `impl Debug` in the current scope
2-
--> $DIR/call_method_on_inherent_impl_ref.rs:20:11
2+
--> $DIR/call_method_on_inherent_impl_ref.rs:19:11
33
|
44
LL | fn my_debug(&self);
55
| -------- the method is available for `&impl Debug` here
@@ -14,27 +14,6 @@ note: `MyDebug` defines an item `my_debug`, perhaps you need to implement it
1414
LL | trait MyDebug {
1515
| ^^^^^^^^^^^^^
1616

17-
error[E0391]: cycle detected when computing type of opaque `my_foo::{opaque#0}`
18-
--> $DIR/call_method_on_inherent_impl_ref.rs:15:16
19-
|
20-
LL | fn my_foo() -> impl std::fmt::Debug {
21-
| ^^^^^^^^^^^^^^^^^^^^
22-
|
23-
note: ...which requires type-checking `my_foo`...
24-
--> $DIR/call_method_on_inherent_impl_ref.rs:20:9
25-
|
26-
LL | x.my_debug();
27-
| ^
28-
= note: ...which requires evaluating trait selection obligation `my_foo::{opaque#0}: core::marker::Unpin`...
29-
= note: ...which again requires computing type of opaque `my_foo::{opaque#0}`, completing the cycle
30-
note: cycle used when computing type of `my_foo::{opaque#0}`
31-
--> $DIR/call_method_on_inherent_impl_ref.rs:15:16
32-
|
33-
LL | fn my_foo() -> impl std::fmt::Debug {
34-
| ^^^^^^^^^^^^^^^^^^^^
35-
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
36-
37-
error: aborting due to 2 previous errors
17+
error: aborting due to 1 previous error
3818

39-
Some errors have detailed explanations: E0391, E0599.
40-
For more information about an error, try `rustc --explain E0391`.
19+
For more information about this error, try `rustc --explain E0599`.

tests/ui/impl-trait/call_method_on_inherent_impl_ref.next.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/call_method_on_inherent_impl_ref.rs:18:13
2+
--> $DIR/call_method_on_inherent_impl_ref.rs:17:13
33
|
44
LL | let x = my_foo();
55
| ^
@@ -13,7 +13,7 @@ LL | let x: /* Type */ = my_foo();
1313
| ++++++++++++
1414

1515
error[E0282]: type annotations needed for `&_`
16-
--> $DIR/call_method_on_inherent_impl_ref.rs:28:13
16+
--> $DIR/call_method_on_inherent_impl_ref.rs:27:13
1717
|
1818
LL | let x = &my_bar();
1919
| ^

tests/ui/impl-trait/call_method_on_inherent_impl_ref.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ where
1313
}
1414

1515
fn my_foo() -> impl std::fmt::Debug {
16-
//[current]~^ cycle
1716
if false {
1817
let x = my_foo();
1918
//[next]~^ type annotations needed

tests/ui/impl-trait/rpit/const_check_false_cycle.current.stderr

-34
This file was deleted.

0 commit comments

Comments
 (0)