1
Fork 0

Rollup merge of #136526 - Zalathar:thir-cx, r=Nadrieril

mir_build: Rename `thir::cx::Cx` to `ThirBuildCx` and remove `UserAnnotatedTyHelpers`

A combination of two loosely-related tweaks that would otherwise conflict with each other:

- `Cx` is a pretty unhelpful type name, especially when jumping between THIR-building and MIR-building while trying to make changes to THIR data structures.

- The `UserAnnotatedTyHelpers` trait doesn't appear to provide any benefit over a simple helper function, and its `tcx()` method is currently completely unnecessary.

No functional change.
This commit is contained in:
Matthias Krüger 2025-02-04 18:49:42 +01:00 committed by GitHub
commit 1b7efa285d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 86 additions and 109 deletions

View file

@ -6,9 +6,9 @@ use rustc_middle::ty;
use rustc_middle::ty::CanonicalUserTypeAnnotation; use rustc_middle::ty::CanonicalUserTypeAnnotation;
use tracing::debug; use tracing::debug;
use crate::thir::cx::Cx; use crate::thir::cx::ThirBuildCx;
impl<'tcx> Cx<'tcx> { impl<'tcx> ThirBuildCx<'tcx> {
pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> BlockId { pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> BlockId {
// We have to eagerly lower the "spine" of the statements // We have to eagerly lower the "spine" of the statements
// in order to get the lexical scoping correctly. // in order to get the lexical scoping correctly.

View file

@ -21,10 +21,9 @@ use rustc_middle::{bug, span_bug};
use rustc_span::{Span, sym}; use rustc_span::{Span, sym};
use tracing::{debug, info, instrument, trace}; use tracing::{debug, info, instrument, trace};
use crate::thir::cx::Cx; use crate::thir::cx::ThirBuildCx;
use crate::thir::util::UserAnnotatedTyHelpers;
impl<'tcx> Cx<'tcx> { impl<'tcx> ThirBuildCx<'tcx> {
/// Create a THIR expression for the given HIR expression. This expands all /// Create a THIR expression for the given HIR expression. This expands all
/// adjustments and directly adds the type information from the /// adjustments and directly adds the type information from the
/// `typeck_results`. See the [dev-guide] for more details. /// `typeck_results`. See the [dev-guide] for more details.
@ -142,9 +141,9 @@ impl<'tcx> Cx<'tcx> {
Adjust::Deref(Some(deref)) => { Adjust::Deref(Some(deref)) => {
// We don't need to do call adjust_span here since // We don't need to do call adjust_span here since
// deref coercions always start with a built-in deref. // deref coercions always start with a built-in deref.
let call_def_id = deref.method_call(self.tcx()); let call_def_id = deref.method_call(self.tcx);
let overloaded_callee = let overloaded_callee =
Ty::new_fn_def(self.tcx(), call_def_id, self.tcx().mk_args(&[expr.ty.into()])); Ty::new_fn_def(self.tcx, call_def_id, self.tcx.mk_args(&[expr.ty.into()]));
expr = Expr { expr = Expr {
temp_lifetime, temp_lifetime,
@ -253,10 +252,10 @@ impl<'tcx> Cx<'tcx> {
// Check to see if this cast is a "coercion cast", where the cast is actually done // Check to see if this cast is a "coercion cast", where the cast is actually done
// using a coercion (or is a no-op). // using a coercion (or is a no-op).
if self.typeck_results().is_coercion_cast(source.hir_id) { if self.typeck_results.is_coercion_cast(source.hir_id) {
// Convert the lexpr to a vexpr. // Convert the lexpr to a vexpr.
ExprKind::Use { source: self.mirror_expr(source) } ExprKind::Use { source: self.mirror_expr(source) }
} else if self.typeck_results().expr_ty(source).is_ref() { } else if self.typeck_results.expr_ty(source).is_ref() {
// Special cased so that we can type check that the element // Special cased so that we can type check that the element
// type of the source matches the pointed to type of the // type of the source matches the pointed to type of the
// destination. // destination.
@ -266,8 +265,8 @@ impl<'tcx> Cx<'tcx> {
is_from_as_cast: true, is_from_as_cast: true,
} }
} else if let hir::ExprKind::Path(ref qpath) = source.kind } else if let hir::ExprKind::Path(ref qpath) = source.kind
&& let res = self.typeck_results().qpath_res(qpath, source.hir_id) && let res = self.typeck_results.qpath_res(qpath, source.hir_id)
&& let ty = self.typeck_results().node_type(source.hir_id) && let ty = self.typeck_results.node_type(source.hir_id)
&& let ty::Adt(adt_def, args) = ty.kind() && let ty::Adt(adt_def, args) = ty.kind()
&& let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res && let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res
{ {
@ -330,7 +329,7 @@ impl<'tcx> Cx<'tcx> {
#[instrument(level = "debug", skip(self), ret)] #[instrument(level = "debug", skip(self), ret)]
fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> { fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
let tcx = self.tcx; let tcx = self.tcx;
let expr_ty = self.typeck_results().expr_ty(expr); let expr_ty = self.typeck_results.expr_ty(expr);
let (temp_lifetime, backwards_incompatible) = let (temp_lifetime, backwards_incompatible) =
self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id); self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
@ -354,7 +353,7 @@ impl<'tcx> Cx<'tcx> {
} }
hir::ExprKind::Call(fun, ref args) => { hir::ExprKind::Call(fun, ref args) => {
if self.typeck_results().is_method_call(expr) { if self.typeck_results.is_method_call(expr) {
// The callee is something implementing Fn, FnMut, or FnOnce. // The callee is something implementing Fn, FnMut, or FnOnce.
// Find the actual method implementation being called and // Find the actual method implementation being called and
// build the appropriate UFCS call expression with the // build the appropriate UFCS call expression with the
@ -364,7 +363,7 @@ impl<'tcx> Cx<'tcx> {
let method = self.method_callee(expr, fun.span, None); let method = self.method_callee(expr, fun.span, None);
let arg_tys = args.iter().map(|e| self.typeck_results().expr_ty_adjusted(e)); let arg_tys = args.iter().map(|e| self.typeck_results.expr_ty_adjusted(e));
let tupled_args = Expr { let tupled_args = Expr {
ty: Ty::new_tup_from_iter(tcx, arg_tys), ty: Ty::new_tup_from_iter(tcx, arg_tys),
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible }, temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
@ -380,7 +379,7 @@ impl<'tcx> Cx<'tcx> {
from_hir_call: true, from_hir_call: true,
fn_span: expr.span, fn_span: expr.span,
} }
} else if let ty::FnDef(def_id, _) = self.typeck_results().expr_ty(fun).kind() } else if let ty::FnDef(def_id, _) = self.typeck_results.expr_ty(fun).kind()
&& let Some(intrinsic) = self.tcx.intrinsic(def_id) && let Some(intrinsic) = self.tcx.intrinsic(def_id)
&& intrinsic.name == sym::box_new && intrinsic.name == sym::box_new
{ {
@ -413,7 +412,7 @@ impl<'tcx> Cx<'tcx> {
}, },
hir::QPath::TypeRelative(_ty, _) => { hir::QPath::TypeRelative(_ty, _) => {
if let Some((DefKind::Ctor(_, CtorKind::Fn), ctor_id)) = if let Some((DefKind::Ctor(_, CtorKind::Fn), ctor_id)) =
self.typeck_results().type_dependent_def(fun.hir_id) self.typeck_results.type_dependent_def(fun.hir_id)
{ {
Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id))) Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
} else { } else {
@ -426,8 +425,8 @@ impl<'tcx> Cx<'tcx> {
None None
}; };
if let Some((adt_def, index)) = adt_data { if let Some((adt_def, index)) = adt_data {
let node_args = self.typeck_results().node_args(fun.hir_id); let node_args = self.typeck_results.node_args(fun.hir_id);
let user_provided_types = self.typeck_results().user_provided_types(); let user_provided_types = self.typeck_results.user_provided_types();
let user_ty = let user_ty =
user_provided_types.get(fun.hir_id).copied().map(|mut u_ty| { user_provided_types.get(fun.hir_id).copied().map(|mut u_ty| {
if let ty::UserTypeKind::TypeOf(ref mut did, _) = if let ty::UserTypeKind::TypeOf(ref mut did, _) =
@ -457,7 +456,7 @@ impl<'tcx> Cx<'tcx> {
})) }))
} else { } else {
ExprKind::Call { ExprKind::Call {
ty: self.typeck_results().node_type(fun.hir_id), ty: self.typeck_results.node_type(fun.hir_id),
fun: self.mirror_expr(fun), fun: self.mirror_expr(fun),
args: self.mirror_exprs(args), args: self.mirror_exprs(args),
from_hir_call: true, from_hir_call: true,
@ -482,7 +481,7 @@ impl<'tcx> Cx<'tcx> {
} }
hir::ExprKind::AssignOp(op, lhs, rhs) => { hir::ExprKind::AssignOp(op, lhs, rhs) => {
if self.typeck_results().is_method_call(expr) { if self.typeck_results.is_method_call(expr) {
let lhs = self.mirror_expr(lhs); let lhs = self.mirror_expr(lhs);
let rhs = self.mirror_expr(rhs); let rhs = self.mirror_expr(rhs);
self.overloaded_operator(expr, Box::new([lhs, rhs])) self.overloaded_operator(expr, Box::new([lhs, rhs]))
@ -498,7 +497,7 @@ impl<'tcx> Cx<'tcx> {
hir::ExprKind::Lit(lit) => ExprKind::Literal { lit, neg: false }, hir::ExprKind::Lit(lit) => ExprKind::Literal { lit, neg: false },
hir::ExprKind::Binary(op, lhs, rhs) => { hir::ExprKind::Binary(op, lhs, rhs) => {
if self.typeck_results().is_method_call(expr) { if self.typeck_results.is_method_call(expr) {
let lhs = self.mirror_expr(lhs); let lhs = self.mirror_expr(lhs);
let rhs = self.mirror_expr(rhs); let rhs = self.mirror_expr(rhs);
self.overloaded_operator(expr, Box::new([lhs, rhs])) self.overloaded_operator(expr, Box::new([lhs, rhs]))
@ -527,7 +526,7 @@ impl<'tcx> Cx<'tcx> {
} }
hir::ExprKind::Index(lhs, index, brackets_span) => { hir::ExprKind::Index(lhs, index, brackets_span) => {
if self.typeck_results().is_method_call(expr) { if self.typeck_results.is_method_call(expr) {
let lhs = self.mirror_expr(lhs); let lhs = self.mirror_expr(lhs);
let index = self.mirror_expr(index); let index = self.mirror_expr(index);
self.overloaded_place( self.overloaded_place(
@ -543,7 +542,7 @@ impl<'tcx> Cx<'tcx> {
} }
hir::ExprKind::Unary(hir::UnOp::Deref, arg) => { hir::ExprKind::Unary(hir::UnOp::Deref, arg) => {
if self.typeck_results().is_method_call(expr) { if self.typeck_results.is_method_call(expr) {
let arg = self.mirror_expr(arg); let arg = self.mirror_expr(arg);
self.overloaded_place(expr, expr_ty, None, Box::new([arg]), expr.span) self.overloaded_place(expr, expr_ty, None, Box::new([arg]), expr.span)
} else { } else {
@ -552,7 +551,7 @@ impl<'tcx> Cx<'tcx> {
} }
hir::ExprKind::Unary(hir::UnOp::Not, arg) => { hir::ExprKind::Unary(hir::UnOp::Not, arg) => {
if self.typeck_results().is_method_call(expr) { if self.typeck_results.is_method_call(expr) {
let arg = self.mirror_expr(arg); let arg = self.mirror_expr(arg);
self.overloaded_operator(expr, Box::new([arg])) self.overloaded_operator(expr, Box::new([arg]))
} else { } else {
@ -561,7 +560,7 @@ impl<'tcx> Cx<'tcx> {
} }
hir::ExprKind::Unary(hir::UnOp::Neg, arg) => { hir::ExprKind::Unary(hir::UnOp::Neg, arg) => {
if self.typeck_results().is_method_call(expr) { if self.typeck_results.is_method_call(expr) {
let arg = self.mirror_expr(arg); let arg = self.mirror_expr(arg);
self.overloaded_operator(expr, Box::new([arg])) self.overloaded_operator(expr, Box::new([arg]))
} else if let hir::ExprKind::Lit(lit) = arg.kind { } else if let hir::ExprKind::Lit(lit) = arg.kind {
@ -574,7 +573,7 @@ impl<'tcx> Cx<'tcx> {
hir::ExprKind::Struct(qpath, fields, ref base) => match expr_ty.kind() { hir::ExprKind::Struct(qpath, fields, ref base) => match expr_ty.kind() {
ty::Adt(adt, args) => match adt.adt_kind() { ty::Adt(adt, args) => match adt.adt_kind() {
AdtKind::Struct | AdtKind::Union => { AdtKind::Struct | AdtKind::Union => {
let user_provided_types = self.typeck_results().user_provided_types(); let user_provided_types = self.typeck_results.user_provided_types();
let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new); let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty); debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty);
ExprKind::Adt(Box::new(AdtExpr { ExprKind::Adt(Box::new(AdtExpr {
@ -586,15 +585,14 @@ impl<'tcx> Cx<'tcx> {
base: match base { base: match base {
hir::StructTailExpr::Base(base) => AdtExprBase::Base(FruInfo { hir::StructTailExpr::Base(base) => AdtExprBase::Base(FruInfo {
base: self.mirror_expr(base), base: self.mirror_expr(base),
field_types: self.typeck_results().fru_field_types() field_types: self.typeck_results.fru_field_types()[expr.hir_id]
[expr.hir_id]
.iter() .iter()
.copied() .copied()
.collect(), .collect(),
}), }),
hir::StructTailExpr::DefaultFields(_) => { hir::StructTailExpr::DefaultFields(_) => {
AdtExprBase::DefaultFields( AdtExprBase::DefaultFields(
self.typeck_results().fru_field_types()[expr.hir_id] self.typeck_results.fru_field_types()[expr.hir_id]
.iter() .iter()
.copied() .copied()
.collect(), .collect(),
@ -605,7 +603,7 @@ impl<'tcx> Cx<'tcx> {
})) }))
} }
AdtKind::Enum => { AdtKind::Enum => {
let res = self.typeck_results().qpath_res(qpath, expr.hir_id); let res = self.typeck_results.qpath_res(qpath, expr.hir_id);
match res { match res {
Res::Def(DefKind::Variant, variant_id) => { Res::Def(DefKind::Variant, variant_id) => {
assert!(matches!( assert!(matches!(
@ -615,8 +613,7 @@ impl<'tcx> Cx<'tcx> {
)); ));
let index = adt.variant_index_with_id(variant_id); let index = adt.variant_index_with_id(variant_id);
let user_provided_types = let user_provided_types = self.typeck_results.user_provided_types();
self.typeck_results().user_provided_types();
let user_ty = let user_ty =
user_provided_types.get(expr.hir_id).copied().map(Box::new); user_provided_types.get(expr.hir_id).copied().map(Box::new);
debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty); debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty);
@ -629,8 +626,7 @@ impl<'tcx> Cx<'tcx> {
base: match base { base: match base {
hir::StructTailExpr::DefaultFields(_) => { hir::StructTailExpr::DefaultFields(_) => {
AdtExprBase::DefaultFields( AdtExprBase::DefaultFields(
self.typeck_results().fru_field_types() self.typeck_results.fru_field_types()[expr.hir_id]
[expr.hir_id]
.iter() .iter()
.copied() .copied()
.collect(), .collect(),
@ -655,7 +651,7 @@ impl<'tcx> Cx<'tcx> {
}, },
hir::ExprKind::Closure { .. } => { hir::ExprKind::Closure { .. } => {
let closure_ty = self.typeck_results().expr_ty(expr); let closure_ty = self.typeck_results.expr_ty(expr);
let (def_id, args, movability) = match *closure_ty.kind() { let (def_id, args, movability) = match *closure_ty.kind() {
ty::Closure(def_id, args) => (def_id, UpvarArgs::Closure(args), None), ty::Closure(def_id, args) => (def_id, UpvarArgs::Closure(args), None),
ty::Coroutine(def_id, args) => { ty::Coroutine(def_id, args) => {
@ -703,7 +699,7 @@ impl<'tcx> Cx<'tcx> {
} }
hir::ExprKind::Path(ref qpath) => { hir::ExprKind::Path(ref qpath) => {
let res = self.typeck_results().qpath_res(qpath, expr.hir_id); let res = self.typeck_results.qpath_res(qpath, expr.hir_id);
self.convert_path_expr(expr, res) self.convert_path_expr(expr, res)
} }
@ -772,7 +768,7 @@ impl<'tcx> Cx<'tcx> {
} }
hir::ExprKind::ConstBlock(ref anon_const) => { hir::ExprKind::ConstBlock(ref anon_const) => {
let ty = self.typeck_results().node_type(anon_const.hir_id); let ty = self.typeck_results.node_type(anon_const.hir_id);
let did = anon_const.def_id.to_def_id(); let did = anon_const.def_id.to_def_id();
let typeck_root_def_id = tcx.typeck_root_def_id(did); let typeck_root_def_id = tcx.typeck_root_def_id(did);
let parent_args = let parent_args =
@ -783,7 +779,7 @@ impl<'tcx> Cx<'tcx> {
} }
// Now comes the rote stuff: // Now comes the rote stuff:
hir::ExprKind::Repeat(v, _) => { hir::ExprKind::Repeat(v, _) => {
let ty = self.typeck_results().expr_ty(expr); let ty = self.typeck_results.expr_ty(expr);
let ty::Array(_, count) = ty.kind() else { let ty::Array(_, count) = ty.kind() else {
span_bug!(expr.span, "unexpected repeat expr ty: {:?}", ty); span_bug!(expr.span, "unexpected repeat expr ty: {:?}", ty);
}; };
@ -837,7 +833,7 @@ impl<'tcx> Cx<'tcx> {
match_source, match_source,
}, },
hir::ExprKind::Loop(body, ..) => { hir::ExprKind::Loop(body, ..) => {
let block_ty = self.typeck_results().node_type(body.hir_id); let block_ty = self.typeck_results.node_type(body.hir_id);
let (temp_lifetime, backwards_incompatible) = self let (temp_lifetime, backwards_incompatible) = self
.rvalue_scopes .rvalue_scopes
.temporary_scope(self.region_scope_tree, body.hir_id.local_id); .temporary_scope(self.region_scope_tree, body.hir_id.local_id);
@ -957,7 +953,7 @@ impl<'tcx> Cx<'tcx> {
| Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
| Res::Def(DefKind::Const, _) | Res::Def(DefKind::Const, _)
| Res::Def(DefKind::AssocConst, _) => { | Res::Def(DefKind::AssocConst, _) => {
self.typeck_results().user_provided_types().get(hir_id).copied().map(Box::new) self.typeck_results.user_provided_types().get(hir_id).copied().map(Box::new)
} }
// A unit struct/variant which is used as a value (e.g., // A unit struct/variant which is used as a value (e.g.,
@ -989,17 +985,13 @@ impl<'tcx> Cx<'tcx> {
Some(fn_def) => (fn_def, None), Some(fn_def) => (fn_def, None),
None => { None => {
let (kind, def_id) = let (kind, def_id) =
self.typeck_results().type_dependent_def(expr.hir_id).unwrap_or_else(|| { self.typeck_results.type_dependent_def(expr.hir_id).unwrap_or_else(|| {
span_bug!(expr.span, "no type-dependent def for method callee") span_bug!(expr.span, "no type-dependent def for method callee")
}); });
let user_ty = self.user_args_applied_to_res(expr.hir_id, Res::Def(kind, def_id)); let user_ty = self.user_args_applied_to_res(expr.hir_id, Res::Def(kind, def_id));
debug!("method_callee: user_ty={:?}", user_ty); debug!("method_callee: user_ty={:?}", user_ty);
( (
Ty::new_fn_def( Ty::new_fn_def(self.tcx, def_id, self.typeck_results.node_args(expr.hir_id)),
self.tcx(),
def_id,
self.typeck_results().node_args(expr.hir_id),
),
user_ty, user_ty,
) )
} }
@ -1025,7 +1017,7 @@ impl<'tcx> Cx<'tcx> {
} }
fn convert_path_expr(&mut self, expr: &'tcx hir::Expr<'tcx>, res: Res) -> ExprKind<'tcx> { fn convert_path_expr(&mut self, expr: &'tcx hir::Expr<'tcx>, res: Res) -> ExprKind<'tcx> {
let args = self.typeck_results().node_args(expr.hir_id); let args = self.typeck_results.node_args(expr.hir_id);
match res { match res {
// A regular function, constructor function or a constant. // A regular function, constructor function or a constant.
Res::Def(DefKind::Fn, _) Res::Def(DefKind::Fn, _)
@ -1060,7 +1052,7 @@ impl<'tcx> Cx<'tcx> {
let user_provided_types = self.typeck_results.user_provided_types(); let user_provided_types = self.typeck_results.user_provided_types();
let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new); let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
debug!("convert_path_expr: user_ty={:?}", user_ty); debug!("convert_path_expr: user_ty={:?}", user_ty);
let ty = self.typeck_results().node_type(expr.hir_id); let ty = self.typeck_results.node_type(expr.hir_id);
match ty.kind() { match ty.kind() {
// A unit struct/variant which is used as a value. // A unit struct/variant which is used as a value.
// We return a completely different ExprKind here to account for this special case. // We return a completely different ExprKind here to account for this special case.

View file

@ -16,15 +16,15 @@ use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
use tracing::instrument; use tracing::instrument;
use crate::thir::pattern::pat_from_hir; use crate::thir::pattern::pat_from_hir;
use crate::thir::util::UserAnnotatedTyHelpers;
/// Query implementation for [`TyCtxt::thir_body`].
pub(crate) fn thir_body( pub(crate) fn thir_body(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
owner_def: LocalDefId, owner_def: LocalDefId,
) -> Result<(&Steal<Thir<'_>>, ExprId), ErrorGuaranteed> { ) -> Result<(&Steal<Thir<'_>>, ExprId), ErrorGuaranteed> {
let hir = tcx.hir(); let hir = tcx.hir();
let body = hir.body_owned_by(owner_def); let body = hir.body_owned_by(owner_def);
let mut cx = Cx::new(tcx, owner_def); let mut cx = ThirBuildCx::new(tcx, owner_def);
if let Some(reported) = cx.typeck_results.tainted_by_errors { if let Some(reported) = cx.typeck_results.tainted_by_errors {
return Err(reported); return Err(reported);
} }
@ -52,8 +52,10 @@ pub(crate) fn thir_body(
Ok((tcx.alloc_steal_thir(cx.thir), expr)) Ok((tcx.alloc_steal_thir(cx.thir), expr))
} }
struct Cx<'tcx> { /// Context for lowering HIR to THIR for a single function body (or other kind of body).
struct ThirBuildCx<'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
/// The THIR data that this context is building.
thir: Thir<'tcx>, thir: Thir<'tcx>,
typing_env: ty::TypingEnv<'tcx>, typing_env: ty::TypingEnv<'tcx>,
@ -69,8 +71,8 @@ struct Cx<'tcx> {
body_owner: DefId, body_owner: DefId,
} }
impl<'tcx> Cx<'tcx> { impl<'tcx> ThirBuildCx<'tcx> {
fn new(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Cx<'tcx> { fn new(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Self {
let typeck_results = tcx.typeck(def); let typeck_results = tcx.typeck(def);
let hir = tcx.hir(); let hir = tcx.hir();
let hir_id = tcx.local_def_id_to_hir_id(def); let hir_id = tcx.local_def_id_to_hir_id(def);
@ -94,7 +96,7 @@ impl<'tcx> Cx<'tcx> {
BodyTy::Const(typeck_results.node_type(hir_id)) BodyTy::Const(typeck_results.node_type(hir_id))
}; };
Cx { Self {
tcx, tcx,
thir: Thir::new(body_type), thir: Thir::new(body_type),
// FIXME(#132279): We're in a body, we should use a typing // FIXME(#132279): We're in a body, we should use a typing
@ -113,7 +115,7 @@ impl<'tcx> Cx<'tcx> {
#[instrument(level = "debug", skip(self))] #[instrument(level = "debug", skip(self))]
fn pattern_from_hir(&mut self, p: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> { fn pattern_from_hir(&mut self, p: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
pat_from_hir(self.tcx, self.typing_env, self.typeck_results(), p) pat_from_hir(self.tcx, self.typing_env, self.typeck_results, p)
} }
fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> { fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {
@ -197,15 +199,12 @@ impl<'tcx> Cx<'tcx> {
Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) } Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) }
}) })
} }
}
impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'tcx> { fn user_args_applied_to_ty_of_hir_id(
fn tcx(&self) -> TyCtxt<'tcx> { &self,
self.tcx hir_id: HirId,
} ) -> Option<ty::CanonicalUserType<'tcx>> {
crate::thir::util::user_args_applied_to_ty_of_hir_id(self.typeck_results, hir_id)
fn typeck_results(&self) -> &ty::TypeckResults<'tcx> {
self.typeck_results
} }
} }

View file

@ -27,7 +27,6 @@ use tracing::{debug, instrument};
pub(crate) use self::check_match::check_match; pub(crate) use self::check_match::check_match;
use crate::errors::*; use crate::errors::*;
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use crate::thir::util::UserAnnotatedTyHelpers;
struct PatCtxt<'a, 'tcx> { struct PatCtxt<'a, 'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
@ -540,16 +539,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
_ => { _ => {
let e = match res { let e = match res {
Res::Def(DefKind::ConstParam, def_id) => { Res::Def(DefKind::ConstParam, def_id) => {
self.tcx.dcx().emit_err(ConstParamInPattern { let const_span = self.tcx.def_span(def_id);
span, self.tcx.dcx().emit_err(ConstParamInPattern { span, const_span })
const_span: self.tcx().def_span(def_id),
})
} }
Res::Def(DefKind::Static { .. }, def_id) => { Res::Def(DefKind::Static { .. }, def_id) => {
self.tcx.dcx().emit_err(StaticInPattern { let static_span = self.tcx.def_span(def_id);
span, self.tcx.dcx().emit_err(StaticInPattern { span, static_span })
static_span: self.tcx().def_span(def_id),
})
} }
_ => self.tcx.dcx().emit_err(NonConstPath { span }), _ => self.tcx.dcx().emit_err(NonConstPath { span }),
}; };
@ -573,6 +568,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
kind kind
} }
fn user_args_applied_to_ty_of_hir_id(
&self,
hir_id: hir::HirId,
) -> Option<ty::CanonicalUserType<'tcx>> {
crate::thir::util::user_args_applied_to_ty_of_hir_id(self.typeck_results, hir_id)
}
/// Takes a HIR Path. If the path is a constant, evaluates it and feeds /// Takes a HIR Path. If the path is a constant, evaluates it and feeds
/// it to `const_to_pat`. Any other path (like enum variants without fields) /// it to `const_to_pat`. Any other path (like enum variants without fields)
/// is converted to the corresponding pattern via `lower_variant_or_leaf`. /// is converted to the corresponding pattern via `lower_variant_or_leaf`.
@ -603,12 +605,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
return pattern; return pattern;
} }
let user_provided_types = self.typeck_results().user_provided_types(); let user_provided_types = self.typeck_results.user_provided_types();
if let Some(&user_ty) = user_provided_types.get(id) { if let Some(&user_ty) = user_provided_types.get(id) {
let annotation = CanonicalUserTypeAnnotation { let annotation = CanonicalUserTypeAnnotation {
user_ty: Box::new(user_ty), user_ty: Box::new(user_ty),
span, span,
inferred_ty: self.typeck_results().node_type(id), inferred_ty: self.typeck_results.node_type(id),
}; };
Box::new(Pat { Box::new(Pat {
span, span,
@ -672,13 +674,3 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
self.const_to_pat(constant, ct_ty, expr.hir_id, lit.span).kind self.const_to_pat(constant, ct_ty, expr.hir_id, lit.span).kind
} }
} }
impl<'tcx> UserAnnotatedTyHelpers<'tcx> for PatCtxt<'_, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn typeck_results(&self) -> &ty::TypeckResults<'tcx> {
self.typeck_results
}
}

View file

@ -1,24 +1,19 @@
use rustc_hir as hir; use rustc_hir as hir;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::ty::{self, CanonicalUserType, TyCtxt}; use rustc_middle::ty::{self, CanonicalUserType};
use tracing::debug; use tracing::debug;
pub(crate) trait UserAnnotatedTyHelpers<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx>;
fn typeck_results(&self) -> &ty::TypeckResults<'tcx>;
/// Looks up the type associated with this hir-id and applies the /// Looks up the type associated with this hir-id and applies the
/// user-given generic parameters; the hir-id must map to a suitable /// user-given generic parameters; the hir-id must map to a suitable
/// type. /// type.
fn user_args_applied_to_ty_of_hir_id( pub(crate) fn user_args_applied_to_ty_of_hir_id<'tcx>(
&self, typeck_results: &ty::TypeckResults<'tcx>,
hir_id: hir::HirId, hir_id: hir::HirId,
) -> Option<CanonicalUserType<'tcx>> { ) -> Option<CanonicalUserType<'tcx>> {
let user_provided_types = self.typeck_results().user_provided_types(); let user_provided_types = typeck_results.user_provided_types();
let mut user_ty = *user_provided_types.get(hir_id)?; let mut user_ty = *user_provided_types.get(hir_id)?;
debug!("user_subts_applied_to_ty_of_hir_id: user_ty={:?}", user_ty); debug!("user_subts_applied_to_ty_of_hir_id: user_ty={:?}", user_ty);
let ty = self.typeck_results().node_type(hir_id); let ty = typeck_results.node_type(hir_id);
match ty.kind() { match ty.kind() {
ty::Adt(adt_def, ..) => { ty::Adt(adt_def, ..) => {
if let ty::UserTypeKind::TypeOf(ref mut did, _) = &mut user_ty.value.kind { if let ty::UserTypeKind::TypeOf(ref mut did, _) = &mut user_ty.value.kind {
@ -30,4 +25,3 @@ pub(crate) trait UserAnnotatedTyHelpers<'tcx> {
_ => bug!("ty: {:?} should not have user provided type {:?} recorded ", ty, user_ty), _ => bug!("ty: {:?} should not have user provided type {:?} recorded ", ty, user_ty),
} }
} }
}