Skip to content

Commit

Permalink
Do not try to reveal hidden types when trying to prove Freeze in the …
Browse files Browse the repository at this point in the history
…defining scope
  • Loading branch information
oli-obk committed Mar 8, 2024
1 parent e55d738 commit 69500f9
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 111 deletions.
28 changes: 27 additions & 1 deletion compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,33 @@ impl Qualif for HasMutInterior {
}

fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
!ty.is_freeze(cx.tcx, cx.param_env)
// Avoid selecting for simple cases, such as builtin types.
if ty.is_trivially_freeze() {
return false;
}

// We do not use `ty.is_freeze` here, because that requires revealing opaque types, which
// requires borrowck, which in turn will invoke mir_const_qualifs again, causing a cycle error.
// Instead we invoke an obligation context manually, and provide the opaque type inference settings
// that allow the trait solver to just error out instead of cycling.
let freeze_def_id = cx.tcx.require_lang_item(LangItem::Freeze, Some(cx.body.span));

let obligation = Obligation::new(
cx.tcx,
ObligationCause::dummy_with_span(cx.body.span),
cx.param_env,
ty::TraitRef::new(cx.tcx, freeze_def_id, [ty::GenericArg::from(ty)]),
);

let infcx = cx
.tcx
.infer_ctxt()
.with_opaque_type_inference(cx.body.source.def_id().expect_local())
.build();
let ocx = ObligationCtxt::new(&infcx);
ocx.register_obligation(obligation);
let errors = ocx.select_all_or_error();
!errors.is_empty()
}

fn in_adt_inherently<'tcx>(
Expand Down
8 changes: 1 addition & 7 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,7 @@ impl DiagCtxt {
format!("invalid level in `stash_diagnostic`: {:?}", diag.level),
);
}
Error => {
// This `unchecked_error_guaranteed` is valid. It is where the
// `ErrorGuaranteed` for stashed errors originates. See
// `DiagCtxtInner::drop`.
#[allow(deprecated)]
Some(ErrorGuaranteed::unchecked_error_guaranteed())
}
Error => Some(self.span_delayed_bug(span, "stashing {key:?}")),
DelayedBug => return self.inner.borrow_mut().emit_diagnostic(diag),
ForceWarning(_) | Warning | Note | OnceNote | Help | OnceHelp | FailureNote | Allow
| Expect(_) => None,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ impl<'tcx> Ty<'tcx> {
///
/// Returning true means the type is known to be `Freeze`. Returning
/// `false` means nothing -- could be `Freeze`, might not be.
fn is_trivially_freeze(self) -> bool {
pub fn is_trivially_freeze(self) -> bool {
match self.kind() {
ty::Int(_)
| ty::Uint(_)
Expand Down
21 changes: 14 additions & 7 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2337,13 +2337,20 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
}

ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
// 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));
if let Some(local) = def_id.as_local()
&& self.infcx.opaque_type_origin(local).is_some()
{
// We cannot possibly resolve this opaque type, because we are currently computing its hidden type.
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
} 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));
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/impl-trait/rpit/const_check_false_cycle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! This test causes a cycle error when checking whether the
//! This test caused a cycle error when checking whether the
//! return type is `Freeze` during const checking, even though
//! the information is readily available.
//@ check-pass

const fn f() -> impl Eq {
//~^ ERROR cycle detected
g()
}
const fn g() {}
Expand Down
34 changes: 0 additions & 34 deletions tests/ui/impl-trait/rpit/const_check_false_cycle.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(const_trait_impl, effects)]

const fn test() -> impl ~const Fn() { //~ ERROR `~const` can only be applied to `#[const_trait]` traits
//~^ ERROR cycle detected
const move || { //~ ERROR const closures are experimental
let sl: &[u8] = b"foo";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0658]: const closures are experimental
--> $DIR/ice-112822-expected-type-for-param.rs:5:5
--> $DIR/ice-112822-expected-type-for-param.rs:4:5
|
LL | const move || {
| ^^^^^
Expand All @@ -15,46 +15,15 @@ LL | const fn test() -> impl ~const Fn() {
| ^^^^

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

error[E0391]: cycle detected when computing type of opaque `test::{opaque#0}`
--> $DIR/ice-112822-expected-type-for-param.rs:3:20
|
LL | const fn test() -> impl ~const Fn() {
| ^^^^^^^^^^^^^^^^
|
note: ...which requires borrow-checking `test`...
--> $DIR/ice-112822-expected-type-for-param.rs:3:1
|
LL | const fn test() -> impl ~const Fn() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires promoting constants in MIR for `test`...
--> $DIR/ice-112822-expected-type-for-param.rs:3:1
|
LL | const fn test() -> impl ~const Fn() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires const checking `test`...
--> $DIR/ice-112822-expected-type-for-param.rs:3:1
|
LL | const fn test() -> impl ~const Fn() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing whether `test::{opaque#0}` is freeze...
= note: ...which requires evaluating trait selection obligation `test::{opaque#0}: core::marker::Freeze`...
= note: ...which again requires computing type of opaque `test::{opaque#0}`, completing the cycle
note: cycle used when computing type of `test::{opaque#0}`
--> $DIR/ice-112822-expected-type-for-param.rs:3:20
|
LL | const fn test() -> impl ~const Fn() {
| ^^^^^^^^^^^^^^^^
= 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

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

Some errors have detailed explanations: E0277, E0391, E0658.
Some errors have detailed explanations: E0277, E0658.
For more information about an error, try `rustc --explain E0277`.
1 change: 0 additions & 1 deletion tests/ui/type-alias-impl-trait/in-where-clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![feature(type_alias_impl_trait)]
type Bar = impl Sized;
//~^ ERROR: cycle
//~| ERROR: cycle

fn foo() -> Bar
where
Expand Down
24 changes: 2 additions & 22 deletions tests/ui/type-alias-impl-trait/in-where-clause.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ note: ...which requires computing type of opaque `Bar::{opaque#0}`...
LL | type Bar = impl Sized;
| ^^^^^^^^^^
note: ...which requires type-checking `foo`...
--> $DIR/in-where-clause.rs:9:1
--> $DIR/in-where-clause.rs:8:1
|
LL | / fn foo() -> Bar
LL | | where
Expand All @@ -25,26 +25,6 @@ LL | type Bar = impl Sized;
| ^^^^^^^^^^
= 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

error[E0391]: cycle detected when computing type of opaque `Bar::{opaque#0}`
--> $DIR/in-where-clause.rs:5:12
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
|
note: ...which requires type-checking `foo`...
--> $DIR/in-where-clause.rs:13:9
|
LL | [0; 1 + 2]
| ^^^^^
= note: ...which requires evaluating trait selection obligation `Bar: core::marker::Send`...
= note: ...which again requires computing type of opaque `Bar::{opaque#0}`, completing the cycle
note: cycle used when computing type of `Bar::{opaque#0}`
--> $DIR/in-where-clause.rs:5:12
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
= 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

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

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

0 comments on commit 69500f9

Please sign in to comment.