Skip to content

Commit 82622c6

Browse files
Rollup merge of #133471 - lcnr:uwu-gamer, r=BoxyUwU
gce: fix typing_mode mismatch Fixes #133271 r? `@BoxyUwU`
2 parents 32dc393 + 58936c1 commit 82622c6

File tree

3 files changed

+89
-18
lines changed

3 files changed

+89
-18
lines changed

compiler/rustc_trait_selection/src/traits/mod.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,18 @@ pub fn try_evaluate_const<'tcx>(
551551
| ty::ConstKind::Placeholder(_)
552552
| ty::ConstKind::Expr(_) => Err(EvaluateConstErr::HasGenericsOrInfers),
553553
ty::ConstKind::Unevaluated(uv) => {
554-
// Postpone evaluation of constants that depend on generic parameters or inference variables.
555-
let (args, param_env) = if tcx.features().generic_const_exprs()
554+
// Postpone evaluation of constants that depend on generic parameters or
555+
// inference variables.
556+
//
557+
// We use `TypingMode::PostAnalysis` here which is not *technically* correct
558+
// to be revealing opaque types here as borrowcheck has not run yet. However,
559+
// CTFE itself uses `TypingMode::PostAnalysis` unconditionally even during
560+
// typeck and not doing so has a lot of (undesirable) fallout (#101478, #119821).
561+
// As a result we always use a revealed env when resolving the instance to evaluate.
562+
//
563+
// FIXME: `const_eval_resolve_for_typeck` should probably just modify the env itself
564+
// instead of having this logic here
565+
let (args, typing_env) = if tcx.features().generic_const_exprs()
556566
&& uv.has_non_region_infer()
557567
{
558568
// `feature(generic_const_exprs)` causes anon consts to inherit all parent generics. This can cause
@@ -568,13 +578,17 @@ pub fn try_evaluate_const<'tcx>(
568578
// the generic arguments provided for it, then we should *not* attempt to evaluate it.
569579
return Err(EvaluateConstErr::HasGenericsOrInfers);
570580
} else {
571-
(replace_param_and_infer_args_with_placeholder(tcx, uv.args), param_env)
581+
let args = replace_param_and_infer_args_with_placeholder(tcx, uv.args);
582+
let typing_env = infcx
583+
.typing_env(tcx.erase_regions(param_env))
584+
.with_post_analysis_normalized(tcx);
585+
(args, typing_env)
572586
}
573587
}
574588
Err(_) | Ok(None) => {
575589
let args = GenericArgs::identity_for_item(tcx, uv.def);
576-
let param_env = tcx.param_env(uv.def);
577-
(args, param_env)
590+
let typing_env = ty::TypingEnv::post_analysis(tcx, uv.def);
591+
(args, typing_env)
578592
}
579593
}
580594
} else if tcx.def_kind(uv.def) == DefKind::AnonConst && uv.has_non_region_infer() {
@@ -593,27 +607,20 @@ pub fn try_evaluate_const<'tcx>(
593607
);
594608

595609
let args = GenericArgs::identity_for_item(tcx, uv.def);
596-
let param_env = tcx.param_env(uv.def);
597-
(args, param_env)
610+
let typing_env = ty::TypingEnv::post_analysis(tcx, uv.def);
611+
(args, typing_env)
598612
} else {
599613
// FIXME: This codepath is reachable under `associated_const_equality` and in the
600614
// future will be reachable by `min_generic_const_args`. We should handle inference
601615
// variables and generic parameters properly instead of doing nothing.
602-
(uv.args, param_env)
616+
let typing_env = infcx
617+
.typing_env(tcx.erase_regions(param_env))
618+
.with_post_analysis_normalized(tcx);
619+
(uv.args, typing_env)
603620
};
604621
let uv = ty::UnevaluatedConst::new(uv.def, args);
605622

606-
// It's not *technically* correct to be revealing opaque types here as borrowcheck has
607-
// not run yet. However, CTFE itself uses `TypingMode::PostAnalysis` unconditionally even
608-
// during typeck and not doing so has a lot of (undesirable) fallout (#101478, #119821).
609-
// As a result we always use a revealed env when resolving the instance to evaluate.
610-
//
611-
// FIXME: `const_eval_resolve_for_typeck` should probably just modify the env itself
612-
// instead of having this logic here
613-
let typing_env =
614-
tcx.erase_regions(infcx.typing_env(param_env)).with_post_analysis_normalized(tcx);
615623
let erased_uv = tcx.erase_regions(uv);
616-
617624
use rustc_middle::mir::interpret::ErrorHandled;
618625
match tcx.const_eval_resolve_for_typeck(typing_env, erased_uv, DUMMY_SP) {
619626
Ok(Ok(val)) => Ok(ty::Const::new_value(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Regression test for #133271.
2+
#![feature(generic_const_exprs)]
3+
//~^ WARN the feature `generic_const_exprs` is incomplete
4+
5+
struct Foo;
6+
impl<'a, const NUM: usize> std::ops::Add<&'a Foo> for Foo
7+
//~^ ERROR the const parameter `NUM` is not constrained by the impl trait, self type, or predicates
8+
where
9+
[(); 1 + 0]: Sized,
10+
{
11+
fn unimplemented(self, _: &Foo) -> Self::Output {
12+
//~^ ERROR method `unimplemented` is not a member of trait `std::ops::Add`
13+
//~| ERROR type annotations needed
14+
loop {}
15+
}
16+
}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error[E0407]: method `unimplemented` is not a member of trait `std::ops::Add`
2+
--> $DIR/post-analysis-user-facing-param-env.rs:11:5
3+
|
4+
LL | / fn unimplemented(self, _: &Foo) -> Self::Output {
5+
LL | |
6+
LL | |
7+
LL | | loop {}
8+
LL | | }
9+
| |_____^ not a member of trait `std::ops::Add`
10+
11+
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
12+
--> $DIR/post-analysis-user-facing-param-env.rs:2:12
13+
|
14+
LL | #![feature(generic_const_exprs)]
15+
| ^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
18+
= note: `#[warn(incomplete_features)]` on by default
19+
20+
error[E0207]: the const parameter `NUM` is not constrained by the impl trait, self type, or predicates
21+
--> $DIR/post-analysis-user-facing-param-env.rs:6:10
22+
|
23+
LL | impl<'a, const NUM: usize> std::ops::Add<&'a Foo> for Foo
24+
| ^^^^^^^^^^^^^^^^ unconstrained const parameter
25+
|
26+
= note: expressions using a const parameter must map each value to a distinct output value
27+
= note: proving the result of expressions other than the parameter are unique is not supported
28+
29+
error[E0284]: type annotations needed
30+
--> $DIR/post-analysis-user-facing-param-env.rs:11:40
31+
|
32+
LL | fn unimplemented(self, _: &Foo) -> Self::Output {
33+
| ^^^^^^^^^^^^ cannot infer the value of const parameter `NUM`
34+
|
35+
note: required for `Foo` to implement `Add<&'a Foo>`
36+
--> $DIR/post-analysis-user-facing-param-env.rs:6:28
37+
|
38+
LL | impl<'a, const NUM: usize> std::ops::Add<&'a Foo> for Foo
39+
| ---------------- ^^^^^^^^^^^^^^^^^^^^^^ ^^^
40+
| |
41+
| unsatisfied trait bound introduced here
42+
43+
error: aborting due to 3 previous errors; 1 warning emitted
44+
45+
Some errors have detailed explanations: E0207, E0284, E0407.
46+
For more information about an error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)