Skip to content

Commit f4b516b

Browse files
committed
thir building: use typing_env directly
1 parent 0f8405f commit f4b516b

File tree

5 files changed

+32
-67
lines changed

5 files changed

+32
-67
lines changed

compiler/rustc_mir_build/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NonExhaustivePatternsTypeNo
595595
}
596596

597597
if let ty::Ref(_, sub_ty, _) = self.ty.kind() {
598-
if !sub_ty.is_inhabited_from(self.cx.tcx, self.cx.module, self.cx.typing_env()) {
598+
if !sub_ty.is_inhabited_from(self.cx.tcx, self.cx.module, self.cx.typing_env) {
599599
diag.note(fluent::mir_build_reference_note);
600600
}
601601
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'tcx> Cx<'tcx> {
284284
let discr_ty = ty.to_ty(tcx);
285285

286286
let size = tcx
287-
.layout_of(self.typing_env().as_query_input(discr_ty))
287+
.layout_of(self.typing_env.as_query_input(discr_ty))
288288
.unwrap_or_else(|e| panic!("could not compute layout for {discr_ty:?}: {e:?}"))
289289
.size;
290290

@@ -1040,7 +1040,7 @@ impl<'tcx> Cx<'tcx> {
10401040
// but distinguish between &STATIC and &THREAD_LOCAL as they have different semantics
10411041
Res::Def(DefKind::Static { .. }, id) => {
10421042
// this is &raw for extern static or static mut, and & for other statics
1043-
let ty = self.tcx.static_ptr_ty(id, self.typing_env());
1043+
let ty = self.tcx.static_ptr_ty(id, self.typing_env);
10441044
let (temp_lifetime, backwards_incompatible) = self
10451045
.rvalue_scopes
10461046
.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);

compiler/rustc_mir_build/src/thir/cx/mod.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_hir::lang_items::LangItem;
1212
use rustc_middle::bug;
1313
use rustc_middle::middle::region;
1414
use rustc_middle::thir::*;
15-
use rustc_middle::ty::solve::Reveal;
1615
use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
1716
use tracing::instrument;
1817

@@ -57,7 +56,7 @@ struct Cx<'tcx> {
5756
tcx: TyCtxt<'tcx>,
5857
thir: Thir<'tcx>,
5958

60-
param_env: ty::ParamEnv<'tcx>,
59+
typing_env: ty::TypingEnv<'tcx>,
6160

6261
region_scope_tree: &'tcx region::ScopeTree,
6362
typeck_results: &'tcx ty::TypeckResults<'tcx>,
@@ -98,7 +97,9 @@ impl<'tcx> Cx<'tcx> {
9897
Cx {
9998
tcx,
10099
thir: Thir::new(body_type),
101-
param_env: tcx.param_env(def),
100+
// FIXME(#132279): We're in a body, we should use a typing
101+
// mode which reveals the opaque types defined by that body.
102+
typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
102103
region_scope_tree: tcx.region_scope_tree(def),
103104
typeck_results,
104105
rvalue_scopes: &typeck_results.rvalue_scopes,
@@ -110,20 +111,9 @@ impl<'tcx> Cx<'tcx> {
110111
}
111112
}
112113

113-
fn typing_mode(&self) -> ty::TypingMode<'tcx> {
114-
debug_assert_eq!(self.param_env.reveal(), Reveal::UserFacing);
115-
// FIXME(#132279): In case we're in a body, we should use a typing
116-
// mode which reveals the opaque types defined by that body.
117-
ty::TypingMode::non_body_analysis()
118-
}
119-
120-
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
121-
ty::TypingEnv { typing_mode: self.typing_mode(), param_env: self.param_env }
122-
}
123-
124114
#[instrument(level = "debug", skip(self))]
125115
fn pattern_from_hir(&mut self, p: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
126-
pat_from_hir(self.tcx, self.typing_env(), self.typeck_results(), p)
116+
pat_from_hir(self.tcx, self.typing_env, self.typeck_results(), p)
127117
}
128118

129119
fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_hir::def::*;
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_hir::{self as hir, BindingMode, ByRef, HirId};
1010
use rustc_infer::infer::TyCtxtInferExt;
11-
use rustc_infer::traits::Reveal;
1211
use rustc_lint::Level;
1312
use rustc_middle::bug;
1413
use rustc_middle::middle::limits::get_limit_size;
@@ -43,7 +42,8 @@ pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), Err
4342
tcx,
4443
thir: &*thir,
4544
typeck_results,
46-
param_env: tcx.param_env(def_id),
45+
// FIXME(#132279): We're in a body, should handle opaques.
46+
typing_env: ty::TypingEnv::non_body_analysis(tcx, def_id),
4747
lint_level: tcx.local_def_id_to_hir_id(def_id),
4848
let_source: LetSource::None,
4949
pattern_arena: &pattern_arena,
@@ -90,7 +90,7 @@ enum LetSource {
9090

9191
struct MatchVisitor<'p, 'tcx> {
9292
tcx: TyCtxt<'tcx>,
93-
param_env: ty::ParamEnv<'tcx>,
93+
typing_env: ty::TypingEnv<'tcx>,
9494
typeck_results: &'tcx ty::TypeckResults<'tcx>,
9595
thir: &'p Thir<'tcx>,
9696
lint_level: HirId,
@@ -196,15 +196,6 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
196196
}
197197

198198
impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
199-
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
200-
// FIXME(#132279): We're in a body, should handle opaques.
201-
debug_assert_eq!(self.param_env.reveal(), Reveal::UserFacing);
202-
ty::TypingEnv {
203-
typing_mode: ty::TypingMode::non_body_analysis(),
204-
param_env: self.param_env,
205-
}
206-
}
207-
208199
#[instrument(level = "trace", skip(self, f))]
209200
fn with_let_source(&mut self, let_source: LetSource, f: impl FnOnce(&mut Self)) {
210201
let old_let_source = self.let_source;
@@ -395,7 +386,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
395386
PatCtxt {
396387
tcx: self.tcx,
397388
typeck_results: self.typeck_results,
398-
param_env: self.param_env,
389+
typing_env: self.typing_env,
399390
module: self.tcx.parent_module(self.lint_level).to_def_id(),
400391
dropless_arena: self.dropless_arena,
401392
match_lint_level: self.lint_level,
@@ -749,8 +740,8 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
749740
.variant(*variant_index)
750741
.inhabited_predicate(self.tcx, *adt)
751742
.instantiate(self.tcx, args);
752-
variant_inhabited.apply(self.tcx, cx.typing_env(), cx.module)
753-
&& !variant_inhabited.apply_ignore_module(self.tcx, cx.typing_env())
743+
variant_inhabited.apply(self.tcx, cx.typing_env, cx.module)
744+
&& !variant_inhabited.apply_ignore_module(self.tcx, cx.typing_env)
754745
} else {
755746
false
756747
};
@@ -789,7 +780,7 @@ fn check_borrow_conflicts_in_at_patterns<'tcx>(cx: &MatchVisitor<'_, 'tcx>, pat:
789780
return;
790781
};
791782

792-
let is_binding_by_move = |ty: Ty<'tcx>| !ty.is_copy_modulo_regions(cx.tcx, cx.typing_env());
783+
let is_binding_by_move = |ty: Ty<'tcx>| !ty.is_copy_modulo_regions(cx.tcx, cx.typing_env);
793784

794785
let sess = cx.tcx.sess;
795786

@@ -1054,7 +1045,7 @@ fn find_fallback_pattern_typo<'tcx>(
10541045
let mut inaccessible = vec![];
10551046
let mut imported = vec![];
10561047
let mut imported_spans = vec![];
1057-
let infcx = cx.tcx.infer_ctxt().build(ty::TypingMode::non_body_analysis());
1048+
let (infcx, param_env) = cx.tcx.infer_ctxt().build_with_typing_env(cx.typing_env);
10581049
let parent = cx.tcx.hir().get_parent_item(hir_id);
10591050

10601051
for item in cx.tcx.hir_crate_items(()).free_items() {
@@ -1067,7 +1058,7 @@ fn find_fallback_pattern_typo<'tcx>(
10671058
};
10681059
for res in &path.res {
10691060
if let Res::Def(DefKind::Const, id) = res
1070-
&& infcx.can_eq(cx.param_env, ty, cx.tcx.type_of(id).instantiate_identity())
1061+
&& infcx.can_eq(param_env, ty, cx.tcx.type_of(id).instantiate_identity())
10711062
{
10721063
if cx.tcx.visibility(id).is_accessible_from(parent, cx.tcx) {
10731064
// The original const is accessible, suggest using it directly.
@@ -1088,11 +1079,7 @@ fn find_fallback_pattern_typo<'tcx>(
10881079
}
10891080
}
10901081
if let DefKind::Const = cx.tcx.def_kind(item.owner_id)
1091-
&& infcx.can_eq(
1092-
cx.param_env,
1093-
ty,
1094-
cx.tcx.type_of(item.owner_id).instantiate_identity(),
1095-
)
1082+
&& infcx.can_eq(param_env, ty, cx.tcx.type_of(item.owner_id).instantiate_identity())
10961083
{
10971084
// Look for local consts.
10981085
let item_name = cx.tcx.item_name(item.owner_id.into());
@@ -1311,7 +1298,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
13111298
}
13121299

13131300
if let ty::Ref(_, sub_ty, _) = scrut_ty.kind() {
1314-
if !sub_ty.is_inhabited_from(cx.tcx, cx.module, cx.typing_env()) {
1301+
if !sub_ty.is_inhabited_from(cx.tcx, cx.module, cx.typing_env) {
13151302
err.note("references are always considered inhabited");
13161303
}
13171304
}

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_index::{Idx, IndexVec};
99
use rustc_middle::middle::stability::EvalResult;
1010
use rustc_middle::mir::{self, Const};
1111
use rustc_middle::thir::{self, Pat, PatKind, PatRange, PatRangeBoundary};
12-
use rustc_middle::traits::Reveal;
1312
use rustc_middle::ty::layout::IntegerExt;
1413
use rustc_middle::ty::{
1514
self, FieldDef, OpaqueTypeKey, ScalarInt, Ty, TyCtxt, TypeVisitableExt, VariantDef,
@@ -86,7 +85,7 @@ pub struct RustcPatCtxt<'p, 'tcx: 'p> {
8685
/// not. E.g., `struct Foo { _private: ! }` cannot be seen to be empty
8786
/// outside its module and should not be matchable with an empty match statement.
8887
pub module: DefId,
89-
pub param_env: ty::ParamEnv<'tcx>,
88+
pub typing_env: ty::TypingEnv<'tcx>,
9089
/// To allocate the result of `self.ctor_sub_tys()`
9190
pub dropless_arena: &'p DroplessArena,
9291
/// Lint level at the match.
@@ -109,17 +108,6 @@ impl<'p, 'tcx: 'p> fmt::Debug for RustcPatCtxt<'p, 'tcx> {
109108
}
110109

111110
impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
112-
pub fn typing_mode(&self) -> ty::TypingMode<'tcx> {
113-
debug_assert_eq!(self.param_env.reveal(), Reveal::UserFacing);
114-
// FIXME(#132279): This is inside of a body. If we need to use the `param_env`
115-
// and `typing_mode` we should reveal opaques defined by that body.
116-
ty::TypingMode::non_body_analysis()
117-
}
118-
119-
pub fn typing_env(&self) -> ty::TypingEnv<'tcx> {
120-
ty::TypingEnv { typing_mode: self.typing_mode(), param_env: self.param_env }
121-
}
122-
123111
/// Type inference occasionally gives us opaque types in places where corresponding patterns
124112
/// have more specific types. To avoid inconsistencies as well as detect opaque uninhabited
125113
/// types, we use the corresponding concrete type if possible.
@@ -151,7 +139,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
151139
pub fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
152140
!ty.inhabited_predicate(self.tcx).apply_revealing_opaque(
153141
self.tcx,
154-
self.typing_env(),
142+
self.typing_env,
155143
self.module,
156144
&|key| self.reveal_opaque_key(key),
157145
)
@@ -191,7 +179,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
191179
variant.fields.iter().map(move |field| {
192180
let ty = field.ty(self.tcx, args);
193181
// `field.ty()` doesn't normalize after instantiating.
194-
let ty = self.tcx.normalize_erasing_regions(self.typing_env(), ty);
182+
let ty = self.tcx.normalize_erasing_regions(self.typing_env, ty);
195183
let ty = self.reveal_opaque_ty(ty);
196184
(field, ty)
197185
})
@@ -381,7 +369,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
381369
let is_inhabited = v
382370
.inhabited_predicate(cx.tcx, *def)
383371
.instantiate(cx.tcx, args)
384-
.apply_revealing_opaque(cx.tcx, cx.typing_env(), cx.module, &|key| {
372+
.apply_revealing_opaque(cx.tcx, cx.typing_env, cx.module, &|key| {
385373
cx.reveal_opaque_key(key)
386374
});
387375
// Variants that depend on a disabled unstable feature.
@@ -442,7 +430,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
442430
match bdy {
443431
PatRangeBoundary::NegInfinity => MaybeInfiniteInt::NegInfinity,
444432
PatRangeBoundary::Finite(value) => {
445-
let bits = value.eval_bits(self.tcx, self.typing_env());
433+
let bits = value.eval_bits(self.tcx, self.typing_env);
446434
match *ty.kind() {
447435
ty::Int(ity) => {
448436
let size = Integer::from_int_ty(&self.tcx, ity).size().bits();
@@ -551,15 +539,15 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
551539
PatKind::Constant { value } => {
552540
match ty.kind() {
553541
ty::Bool => {
554-
ctor = match value.try_eval_bool(cx.tcx, cx.typing_env()) {
542+
ctor = match value.try_eval_bool(cx.tcx, cx.typing_env) {
555543
Some(b) => Bool(b),
556544
None => Opaque(OpaqueId::new()),
557545
};
558546
fields = vec![];
559547
arity = 0;
560548
}
561549
ty::Char | ty::Int(_) | ty::Uint(_) => {
562-
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env()) {
550+
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env) {
563551
Some(bits) => {
564552
let x = match *ty.kind() {
565553
ty::Int(ity) => {
@@ -576,7 +564,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
576564
arity = 0;
577565
}
578566
ty::Float(ty::FloatTy::F16) => {
579-
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env()) {
567+
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env) {
580568
Some(bits) => {
581569
use rustc_apfloat::Float;
582570
let value = rustc_apfloat::ieee::Half::from_bits(bits);
@@ -588,7 +576,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
588576
arity = 0;
589577
}
590578
ty::Float(ty::FloatTy::F32) => {
591-
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env()) {
579+
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env) {
592580
Some(bits) => {
593581
use rustc_apfloat::Float;
594582
let value = rustc_apfloat::ieee::Single::from_bits(bits);
@@ -600,7 +588,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
600588
arity = 0;
601589
}
602590
ty::Float(ty::FloatTy::F64) => {
603-
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env()) {
591+
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env) {
604592
Some(bits) => {
605593
use rustc_apfloat::Float;
606594
let value = rustc_apfloat::ieee::Double::from_bits(bits);
@@ -612,7 +600,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
612600
arity = 0;
613601
}
614602
ty::Float(ty::FloatTy::F128) => {
615-
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env()) {
603+
ctor = match value.try_eval_bits(cx.tcx, cx.typing_env) {
616604
Some(bits) => {
617605
use rustc_apfloat::Float;
618606
let value = rustc_apfloat::ieee::Quad::from_bits(bits);
@@ -661,8 +649,8 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
661649
}
662650
ty::Float(fty) => {
663651
use rustc_apfloat::Float;
664-
let lo = lo.as_finite().map(|c| c.eval_bits(cx.tcx, cx.typing_env()));
665-
let hi = hi.as_finite().map(|c| c.eval_bits(cx.tcx, cx.typing_env()));
652+
let lo = lo.as_finite().map(|c| c.eval_bits(cx.tcx, cx.typing_env));
653+
let hi = hi.as_finite().map(|c| c.eval_bits(cx.tcx, cx.typing_env));
666654
match fty {
667655
ty::FloatTy::F16 => {
668656
use rustc_apfloat::ieee::Half;

0 commit comments

Comments
 (0)