Skip to content

Commit 8de5565

Browse files
committed
Do not try to reveal hidden types when trying to prove Freeze in the defining scope
1 parent 869dc35 commit 8de5565

File tree

10 files changed

+48
-107
lines changed

10 files changed

+48
-107
lines changed

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,33 @@ impl Qualif for HasMutInterior {
9898
}
9999

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

104130
fn in_adt_inherently<'tcx>(

compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ impl<'tcx> Ty<'tcx> {
12321232
///
12331233
/// Returning true means the type is known to be `Freeze`. Returning
12341234
/// `false` means nothing -- could be `Freeze`, might not be.
1235-
fn is_trivially_freeze(self) -> bool {
1235+
pub fn is_trivially_freeze(self) -> bool {
12361236
match self.kind() {
12371237
ty::Int(_)
12381238
| ty::Uint(_)

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -2382,13 +2382,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23822382
}
23832383

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

tests/ui/const-generics/opaque_types.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ note: ...which requires const checking `main::{constant#0}`...
109109
|
110110
LL | foo::<42>();
111111
| ^^
112-
= note: ...which requires computing whether `Foo` is freeze...
113-
= note: ...which requires evaluating trait selection obligation `Foo: core::marker::Freeze`...
114112
= note: ...which again requires computing type of opaque `Foo::{opaque#0}`, completing the cycle
115113
note: cycle used when computing type of `Foo::{opaque#0}`
116114
--> $DIR/opaque_types.rs:3:12

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

-34
This file was deleted.

tests/ui/impl-trait/rpit/const_check_false_cycle.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
//! This test causes a cycle error when checking whether the
1+
//! This test caused a cycle error when checking whether the
22
//! return type is `Freeze` during const checking, even though
33
//! the information is readily available.
44
55
//@ revisions: current next
66
//@[next] compile-flags: -Znext-solver
7-
//@[next] check-pass
7+
//@ check-pass
88

99
const fn f() -> impl Eq {
10-
//[current]~^ ERROR cycle detected
1110
g()
1211
}
1312
const fn g() {}

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/ice-112822-expected-type-for-param.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const fn test() -> impl ~const Fn() {
44
//~^ ERROR `~const` can only be applied to `#[const_trait]` traits
55
//~| ERROR `~const` can only be applied to `#[const_trait]` traits
6-
//~| ERROR cycle detected
76
const move || { //~ ERROR const closures are experimental
87
let sl: &[u8] = b"foo";
98

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: const closures are experimental
2-
--> $DIR/ice-112822-expected-type-for-param.rs:7:5
2+
--> $DIR/ice-112822-expected-type-for-param.rs:6:5
33
|
44
LL | const move || {
55
| ^^^^^
@@ -23,46 +23,15 @@ LL | const fn test() -> impl ~const Fn() {
2323
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2424

2525
error[E0277]: can't compare `&u8` with `&u8`
26-
--> $DIR/ice-112822-expected-type-for-param.rs:12:17
26+
--> $DIR/ice-112822-expected-type-for-param.rs:11:17
2727
|
2828
LL | assert_eq!(first, &b'f');
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `&u8 == &u8`
3030
|
3131
= help: the trait `~const PartialEq<&u8>` is not implemented for `&u8`
3232
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
3333

34-
error[E0391]: cycle detected when computing type of opaque `test::{opaque#0}`
35-
--> $DIR/ice-112822-expected-type-for-param.rs:3:20
36-
|
37-
LL | const fn test() -> impl ~const Fn() {
38-
| ^^^^^^^^^^^^^^^^
39-
|
40-
note: ...which requires borrow-checking `test`...
41-
--> $DIR/ice-112822-expected-type-for-param.rs:3:1
42-
|
43-
LL | const fn test() -> impl ~const Fn() {
44-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45-
note: ...which requires promoting constants in MIR for `test`...
46-
--> $DIR/ice-112822-expected-type-for-param.rs:3:1
47-
|
48-
LL | const fn test() -> impl ~const Fn() {
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50-
note: ...which requires const checking `test`...
51-
--> $DIR/ice-112822-expected-type-for-param.rs:3:1
52-
|
53-
LL | const fn test() -> impl ~const Fn() {
54-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55-
= note: ...which requires computing whether `test::{opaque#0}` is freeze...
56-
= note: ...which requires evaluating trait selection obligation `test::{opaque#0}: core::marker::Freeze`...
57-
= note: ...which again requires computing type of opaque `test::{opaque#0}`, completing the cycle
58-
note: cycle used when computing type of `test::{opaque#0}`
59-
--> $DIR/ice-112822-expected-type-for-param.rs:3:20
60-
|
61-
LL | const fn test() -> impl ~const Fn() {
62-
| ^^^^^^^^^^^^^^^^
63-
= 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
64-
65-
error: aborting due to 5 previous errors
34+
error: aborting due to 4 previous errors
6635

67-
Some errors have detailed explanations: E0277, E0391, E0658.
36+
Some errors have detailed explanations: E0277, E0658.
6837
For more information about an error, try `rustc --explain E0277`.

tests/ui/type-alias-impl-trait/in-where-clause.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![feature(type_alias_impl_trait)]
55
type Bar = impl Sized;
66
//~^ ERROR: cycle
7-
//~| ERROR: cycle
87

98
fn foo() -> Bar
109
where

tests/ui/type-alias-impl-trait/in-where-clause.stderr

+2-22
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: ...which requires computing type of opaque `Bar::{opaque#0}`...
1010
LL | type Bar = impl Sized;
1111
| ^^^^^^^^^^
1212
note: ...which requires type-checking `foo`...
13-
--> $DIR/in-where-clause.rs:9:1
13+
--> $DIR/in-where-clause.rs:8:1
1414
|
1515
LL | / fn foo() -> Bar
1616
LL | | where
@@ -25,26 +25,6 @@ LL | type Bar = impl Sized;
2525
| ^^^^^^^^^^
2626
= 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
2727

28-
error[E0391]: cycle detected when computing type of opaque `Bar::{opaque#0}`
29-
--> $DIR/in-where-clause.rs:5:12
30-
|
31-
LL | type Bar = impl Sized;
32-
| ^^^^^^^^^^
33-
|
34-
note: ...which requires type-checking `foo`...
35-
--> $DIR/in-where-clause.rs:13:9
36-
|
37-
LL | [0; 1 + 2]
38-
| ^^^^^
39-
= note: ...which requires evaluating trait selection obligation `Bar: core::marker::Send`...
40-
= note: ...which again requires computing type of opaque `Bar::{opaque#0}`, completing the cycle
41-
note: cycle used when computing type of `Bar::{opaque#0}`
42-
--> $DIR/in-where-clause.rs:5:12
43-
|
44-
LL | type Bar = impl Sized;
45-
| ^^^^^^^^^^
46-
= 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
47-
48-
error: aborting due to 2 previous errors
28+
error: aborting due to 1 previous error
4929

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

0 commit comments

Comments
 (0)