Skip to content

Commit 9304256

Browse files
committed
Misc cleanups
1 parent ed0006a commit 9304256

File tree

4 files changed

+98
-117
lines changed

4 files changed

+98
-117
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! This query borrow-checks the MIR to (further) ensure it is not broken.
1+
//! This crate implemens MIR typeck and MIR borrowck.
22
33
// tidy-alphabetical-start
44
#![allow(internal_features)]
@@ -111,9 +111,9 @@ pub fn provide(providers: &mut Providers) {
111111
*providers = Providers { mir_borrowck, ..*providers };
112112
}
113113

114-
/// Provider for `query mir_borrowck`. Similar to `typeck`, this must
115-
/// only be called for typeck roots which will then borrowck all
116-
/// nested bodies as well.
114+
/// Provider for `query mir_borrowck`. Unlike `typeck`, this must
115+
/// only be called for typeck roots which *similar* to `typeck` will
116+
/// then borrowck all nested bodies as well.
117117
fn mir_borrowck(
118118
tcx: TyCtxt<'_>,
119119
def: LocalDefId,

compiler/rustc_borrowck/src/root_cx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
255255
}
256256

257257
// We now apply the closure requirements of nested bodies modulo
258-
// regions. In case a body does not depend on opaque types, we
258+
// opaques. In case a body does not depend on opaque types, we
259259
// eagerly check its region constraints and use the final closure
260260
// requirements.
261261
//

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! Code to extract the universally quantified regions declared on a
2-
//! function and the relationships between them. For example:
2+
//! function. For example:
33
//!
44
//! ```
55
//! fn foo<'a, 'b, 'c: 'b>() { }
66
//! ```
77
//!
88
//! here we would return a map assigning each of `{'a, 'b, 'c}`
9-
//! to an index, as well as the `FreeRegionMap` which can compute
10-
//! relationships between them.
9+
//! to an index.
1110
//!
1211
//! The code in this file doesn't *do anything* with those results; it
1312
//! just returns them for other code to use.
@@ -271,8 +270,7 @@ impl<'tcx> UniversalRegions<'tcx> {
271270
/// Creates a new and fully initialized `UniversalRegions` that
272271
/// contains indices for all the free regions found in the given
273272
/// MIR -- that is, all the regions that appear in the function's
274-
/// signature. This will also compute the relationships that are
275-
/// known between those regions.
273+
/// signature.
276274
pub(crate) fn new(infcx: &BorrowckInferCtxt<'tcx>, mir_def: LocalDefId) -> Self {
277275
UniversalRegionsBuilder { infcx, mir_def }.build()
278276
}
@@ -648,10 +646,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
648646

649647
BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(..) => {
650648
let identity_args = GenericArgs::identity_for_item(tcx, typeck_root_def_id);
651-
if self.mir_def.to_def_id() == typeck_root_def_id
652-
// Do not ICE when checking default_field_values consts with lifetimes (#135649)
653-
&& DefKind::Field != tcx.def_kind(tcx.parent(typeck_root_def_id))
654-
{
649+
if self.mir_def.to_def_id() == typeck_root_def_id {
655650
let args = self.infcx.replace_free_regions_with_nll_infer_vars(
656651
NllRegionVariableOrigin::FreeRegion,
657652
identity_args,
@@ -699,30 +694,14 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
699694
let tcx = self.infcx.tcx;
700695
let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.to_def_id());
701696
let identity_args = GenericArgs::identity_for_item(tcx, typeck_root_def_id);
702-
let fr_args = match defining_ty {
703-
DefiningTy::Closure(_, args)
704-
| DefiningTy::CoroutineClosure(_, args)
705-
| DefiningTy::Coroutine(_, args)
706-
| DefiningTy::InlineConst(_, args) => {
707-
// In the case of closures, we rely on the fact that
708-
// the first N elements in the ClosureArgs are
709-
// inherited from the `typeck_root_def_id`.
710-
// Therefore, when we zip together (below) with
711-
// `identity_args`, we will get only those regions
712-
// that correspond to early-bound regions declared on
713-
// the `typeck_root_def_id`.
714-
assert!(args.len() >= identity_args.len());
715-
assert_eq!(args.regions().count(), identity_args.regions().count());
716-
args
717-
}
718-
719-
DefiningTy::FnDef(_, args) | DefiningTy::Const(_, args) => args,
720-
721-
DefiningTy::GlobalAsm(_) => ty::List::empty(),
722-
};
697+
let renumbered_args = defining_ty.args();
723698

724699
let global_mapping = iter::once((tcx.lifetimes.re_static, fr_static));
725-
let arg_mapping = iter::zip(identity_args.regions(), fr_args.regions().map(|r| r.as_var()));
700+
// This relies on typeck roots being generics_of parents with their
701+
// parameters at the start of nested bodies' generics.
702+
assert!(renumbered_args.len() >= identity_args.len());
703+
let arg_mapping =
704+
iter::zip(identity_args.regions(), renumbered_args.regions().map(|r| r.as_var()));
726705

727706
UniversalRegionIndices {
728707
indices: global_mapping.chain(arg_mapping).collect(),
@@ -862,8 +841,8 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
862841
};
863842

864843
// FIXME(#129952): We probably want a more principled approach here.
865-
if let Err(terr) = inputs_and_output.skip_binder().error_reported() {
866-
self.infcx.set_tainted_by_errors(terr);
844+
if let Err(e) = inputs_and_output.error_reported() {
845+
self.infcx.set_tainted_by_errors(e);
867846
}
868847

869848
inputs_and_output

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 81 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,92 +10,15 @@ use rustc_middle::ty::util::IntTypeExt;
1010
use rustc_middle::ty::{self, DefiningScopeKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
1111
use rustc_middle::{bug, span_bug};
1212
use rustc_span::{DUMMY_SP, Ident, Span};
13+
use tracing::instrument;
1314

1415
use super::{HirPlaceholderCollector, ItemCtxt, bad_placeholder};
1516
use crate::check::wfcheck::check_static_item;
1617
use crate::hir_ty_lowering::HirTyLowerer;
1718

1819
mod opaque;
1920

20-
fn anon_const_type_of<'tcx>(icx: &ItemCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
21-
use hir::*;
22-
use rustc_middle::ty::Ty;
23-
let tcx = icx.tcx;
24-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
25-
26-
let node = tcx.hir_node(hir_id);
27-
let Node::AnonConst(&AnonConst { span, .. }) = node else {
28-
span_bug!(
29-
tcx.def_span(def_id),
30-
"expected anon const in `anon_const_type_of`, got {node:?}"
31-
);
32-
};
33-
34-
let parent_node_id = tcx.parent_hir_id(hir_id);
35-
let parent_node = tcx.hir_node(parent_node_id);
36-
37-
match parent_node {
38-
// Anon consts "inside" the type system.
39-
Node::ConstArg(&ConstArg {
40-
hir_id: arg_hir_id,
41-
kind: ConstArgKind::Anon(&AnonConst { hir_id: anon_hir_id, .. }),
42-
..
43-
}) if anon_hir_id == hir_id => const_arg_anon_type_of(icx, arg_hir_id, span),
44-
45-
Node::Variant(Variant { disr_expr: Some(e), .. }) if e.hir_id == hir_id => {
46-
tcx.adt_def(tcx.hir_get_parent_item(hir_id)).repr().discr_type().to_ty(tcx)
47-
}
48-
49-
Node::Field(&hir::FieldDef { default: Some(c), def_id: field_def_id, .. })
50-
if c.hir_id == hir_id =>
51-
{
52-
tcx.type_of(field_def_id).instantiate_identity()
53-
}
54-
55-
_ => Ty::new_error_with_message(
56-
tcx,
57-
span,
58-
format!("unexpected anon const parent in type_of(): {parent_node:?}"),
59-
),
60-
}
61-
}
62-
63-
fn const_arg_anon_type_of<'tcx>(icx: &ItemCtxt<'tcx>, arg_hir_id: HirId, span: Span) -> Ty<'tcx> {
64-
use hir::*;
65-
use rustc_middle::ty::Ty;
66-
67-
let tcx = icx.tcx;
68-
69-
match tcx.parent_hir_node(arg_hir_id) {
70-
// Array length const arguments do not have `type_of` fed as there is never a corresponding
71-
// generic parameter definition.
72-
Node::Ty(&hir::Ty { kind: TyKind::Array(_, ref constant), .. })
73-
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
74-
if constant.hir_id == arg_hir_id =>
75-
{
76-
tcx.types.usize
77-
}
78-
79-
Node::TyPat(pat) => {
80-
let node = match tcx.parent_hir_node(pat.hir_id) {
81-
// Or patterns can be nested one level deep
82-
Node::TyPat(p) => tcx.parent_hir_node(p.hir_id),
83-
other => other,
84-
};
85-
let hir::TyKind::Pat(ty, _) = node.expect_ty().kind else { bug!() };
86-
icx.lower_ty(ty)
87-
}
88-
89-
// This is not a `bug!` as const arguments in path segments that did not resolve to anything
90-
// will result in `type_of` never being fed.
91-
_ => Ty::new_error_with_message(
92-
tcx,
93-
span,
94-
"`type_of` called on const argument's anon const before the const argument was lowered",
95-
),
96-
}
97-
}
98-
21+
#[instrument(level = "debug", skip(tcx), ret)]
9922
pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, Ty<'_>> {
10023
use rustc_hir::*;
10124
use rustc_middle::ty::Ty;
@@ -408,6 +331,85 @@ pub(super) fn type_of_opaque_hir_typeck(
408331
}
409332
}
410333

334+
fn anon_const_type_of<'tcx>(icx: &ItemCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
335+
use hir::*;
336+
use rustc_middle::ty::Ty;
337+
let tcx = icx.tcx;
338+
let hir_id = tcx.local_def_id_to_hir_id(def_id);
339+
340+
let node = tcx.hir_node(hir_id);
341+
let Node::AnonConst(&AnonConst { span, .. }) = node else {
342+
span_bug!(
343+
tcx.def_span(def_id),
344+
"expected anon const in `anon_const_type_of`, got {node:?}"
345+
);
346+
};
347+
348+
let parent_node_id = tcx.parent_hir_id(hir_id);
349+
let parent_node = tcx.hir_node(parent_node_id);
350+
351+
match parent_node {
352+
// Anon consts "inside" the type system.
353+
Node::ConstArg(&ConstArg {
354+
hir_id: arg_hir_id,
355+
kind: ConstArgKind::Anon(&AnonConst { hir_id: anon_hir_id, .. }),
356+
..
357+
}) if anon_hir_id == hir_id => const_arg_anon_type_of(icx, arg_hir_id, span),
358+
359+
Node::Variant(Variant { disr_expr: Some(e), .. }) if e.hir_id == hir_id => {
360+
tcx.adt_def(tcx.hir_get_parent_item(hir_id)).repr().discr_type().to_ty(tcx)
361+
}
362+
363+
Node::Field(&hir::FieldDef { default: Some(c), def_id: field_def_id, .. })
364+
if c.hir_id == hir_id =>
365+
{
366+
tcx.type_of(field_def_id).instantiate_identity()
367+
}
368+
369+
_ => Ty::new_error_with_message(
370+
tcx,
371+
span,
372+
format!("unexpected anon const parent in type_of(): {parent_node:?}"),
373+
),
374+
}
375+
}
376+
377+
fn const_arg_anon_type_of<'tcx>(icx: &ItemCtxt<'tcx>, arg_hir_id: HirId, span: Span) -> Ty<'tcx> {
378+
use hir::*;
379+
use rustc_middle::ty::Ty;
380+
381+
let tcx = icx.tcx;
382+
383+
match tcx.parent_hir_node(arg_hir_id) {
384+
// Array length const arguments do not have `type_of` fed as there is never a corresponding
385+
// generic parameter definition.
386+
Node::Ty(&hir::Ty { kind: TyKind::Array(_, ref constant), .. })
387+
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
388+
if constant.hir_id == arg_hir_id =>
389+
{
390+
tcx.types.usize
391+
}
392+
393+
Node::TyPat(pat) => {
394+
let node = match tcx.parent_hir_node(pat.hir_id) {
395+
// Or patterns can be nested one level deep
396+
Node::TyPat(p) => tcx.parent_hir_node(p.hir_id),
397+
other => other,
398+
};
399+
let hir::TyKind::Pat(ty, _) = node.expect_ty().kind else { bug!() };
400+
icx.lower_ty(ty)
401+
}
402+
403+
// This is not a `bug!` as const arguments in path segments that did not resolve to anything
404+
// will result in `type_of` never being fed.
405+
_ => Ty::new_error_with_message(
406+
tcx,
407+
span,
408+
"`type_of` called on const argument's anon const before the const argument was lowered",
409+
),
410+
}
411+
}
412+
411413
fn infer_placeholder_type<'tcx>(
412414
cx: &dyn HirTyLowerer<'tcx>,
413415
def_id: LocalDefId,

0 commit comments

Comments
 (0)