Skip to content

Commit bd9d8ad

Browse files
authored
Unrolled build for #142267
Rollup merge of #142267 - workingjubilee:debug-assert-less-in-ast-lowering, r=oli-obk assert more in release in `rustc_ast_lowering` My understanding of the compiler's architecture is that in the `ast_lowering` crate, we are constructing the HIR as a one-time thing per crate. This is after tokenizing, parsing, resolution, expansion, possible reparsing, reresolution, reexpansion, and so on. In other words, there are many reasons that perf-focused PRs spend a lot of time touching `rustc_parse`, `rustc_expand`, `rustc_ast`, and then `rustc_hir` and "onwards", but `ast_lowering` is a little bit of an odd duck. In this crate, we have a number of debug assertions. Some are clearly expensive checks that seem like they are prohibitive to run in actual optimized compiler builds, but then there are a number that are simple asserts on integer equalities, `is_empty`, or the like. I believe we should do some of them even in release builds, because the correctness gain is worth the performance cost: almost zero.
2 parents 0d6ab20 + dd78c95 commit bd9d8ad

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
6363

6464
for (def_id, info) in lctx.children {
6565
let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
66-
debug_assert!(
66+
assert!(
6767
matches!(owner, hir::MaybeOwner::Phantom),
6868
"duplicate copy of {def_id:?} in lctx.children"
6969
);
@@ -78,7 +78,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
7878
match node {
7979
AstOwner::NonOwner => {}
8080
AstOwner::Crate(c) => {
81-
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
81+
assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
8282
self.with_lctx(CRATE_NODE_ID, |lctx| {
8383
let module = lctx.lower_mod(&c.items, &c.spans);
8484
// FIXME(jdonszelman): is dummy span ever a problem here?
@@ -1160,7 +1160,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11601160
) -> hir::BodyId {
11611161
let body = hir::Body { params, value: self.arena.alloc(value) };
11621162
let id = body.id();
1163-
debug_assert_eq!(id.hir_id.owner, self.current_hir_id_owner);
1163+
assert_eq!(id.hir_id.owner, self.current_hir_id_owner);
11641164
self.bodies.push((id.hir_id.local_id, self.arena.alloc(body)));
11651165
id
11661166
}
@@ -1673,8 +1673,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
16731673
itctx: ImplTraitContext,
16741674
f: impl FnOnce(&mut Self) -> T,
16751675
) -> (&'hir hir::Generics<'hir>, T) {
1676-
debug_assert!(self.impl_trait_defs.is_empty());
1677-
debug_assert!(self.impl_trait_bounds.is_empty());
1676+
assert!(self.impl_trait_defs.is_empty());
1677+
assert!(self.impl_trait_bounds.is_empty());
16781678

16791679
// Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
16801680
// Note: we used to clone these bounds directly onto the type parameter (and avoid lowering

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
523523
span: Span,
524524
) -> LocalDefId {
525525
let parent = self.current_hir_id_owner.def_id;
526-
debug_assert_ne!(node_id, ast::DUMMY_NODE_ID);
526+
assert_ne!(node_id, ast::DUMMY_NODE_ID);
527527
assert!(
528528
self.opt_local_def_id(node_id).is_none(),
529529
"adding a def'n for node-id {:?} and def kind {:?} but a previous def'n exists: {:?}",
@@ -607,10 +607,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
607607
}
608608

609609
let item = f(self);
610-
debug_assert_eq!(owner_id, item.def_id());
610+
assert_eq!(owner_id, item.def_id());
611611
// `f` should have consumed all the elements in these vectors when constructing `item`.
612-
debug_assert!(self.impl_trait_defs.is_empty());
613-
debug_assert!(self.impl_trait_bounds.is_empty());
612+
assert!(self.impl_trait_defs.is_empty());
613+
assert!(self.impl_trait_bounds.is_empty());
614614
let info = self.make_owner_info(item);
615615

616616
self.attrs = current_attrs;
@@ -918,7 +918,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
918918
} else {
919919
let lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span), id);
920920

921-
debug_assert_eq!(id.owner, self.current_hir_id_owner);
921+
assert_eq!(id.owner, self.current_hir_id_owner);
922922
let ret = self.arena.alloc_from_iter(lowered_attrs);
923923

924924
// this is possible if an item contained syntactical attribute,
@@ -956,10 +956,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
956956
}
957957

958958
fn alias_attrs(&mut self, id: HirId, target_id: HirId) {
959-
debug_assert_eq!(id.owner, self.current_hir_id_owner);
960-
debug_assert_eq!(target_id.owner, self.current_hir_id_owner);
959+
assert_eq!(id.owner, self.current_hir_id_owner);
960+
assert_eq!(target_id.owner, self.current_hir_id_owner);
961961
if let Some(&a) = self.attrs.get(&target_id.local_id) {
962-
debug_assert!(!a.is_empty());
962+
assert!(!a.is_empty());
963963
self.attrs.insert(id.local_id, a);
964964
}
965965
}
@@ -1438,7 +1438,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14381438
let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
14391439
self.resolver.get_lifetime_res(t.id)
14401440
{
1441-
debug_assert_eq!(start.plus(1), end);
1441+
assert_eq!(start.plus(1), end);
14421442
start
14431443
} else {
14441444
self.next_node_id()
@@ -1846,16 +1846,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18461846
let res = match res {
18471847
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
18481848
LifetimeRes::Fresh { param, .. } => {
1849-
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1849+
assert_eq!(ident.name, kw::UnderscoreLifetime);
18501850
let param = self.local_def_id(param);
18511851
hir::LifetimeKind::Param(param)
18521852
}
18531853
LifetimeRes::Infer => {
1854-
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1854+
assert_eq!(ident.name, kw::UnderscoreLifetime);
18551855
hir::LifetimeKind::Infer
18561856
}
18571857
LifetimeRes::Static { .. } => {
1858-
debug_assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
1858+
assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
18591859
hir::LifetimeKind::Static
18601860
}
18611861
LifetimeRes::Error => hir::LifetimeKind::Error,
@@ -2285,7 +2285,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22852285
) -> hir::Stmt<'hir> {
22862286
let hir_id = self.next_id();
22872287
if let Some(a) = attrs {
2288-
debug_assert!(!a.is_empty());
2288+
assert!(!a.is_empty());
22892289
self.attrs.insert(hir_id.local_id, a);
22902290
}
22912291
let local = hir::LetStmt {

0 commit comments

Comments
 (0)