Skip to content

Commit a729c24

Browse files
committed
Auto merge of #142267 - workingjubilee:debug-assert-less-in-ast-lowering, r=<try>
[EXPERIMENTAL] assert in release builds 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 simple integer equalities. I am curious if the latter introduce a serious problem if we simply *do* them. r? `@ghost`
2 parents c6a9554 + dd78c95 commit a729c24

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
@@ -503,7 +503,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
503503
span: Span,
504504
) -> LocalDefId {
505505
let parent = self.current_hir_id_owner.def_id;
506-
debug_assert_ne!(node_id, ast::DUMMY_NODE_ID);
506+
assert_ne!(node_id, ast::DUMMY_NODE_ID);
507507
assert!(
508508
self.opt_local_def_id(node_id).is_none(),
509509
"adding a def'n for node-id {:?} and def kind {:?} but a previous def'n exists: {:?}",
@@ -586,10 +586,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
586586
}
587587

588588
let item = f(self);
589-
debug_assert_eq!(owner_id, item.def_id());
589+
assert_eq!(owner_id, item.def_id());
590590
// `f` should have consumed all the elements in these vectors when constructing `item`.
591-
debug_assert!(self.impl_trait_defs.is_empty());
592-
debug_assert!(self.impl_trait_bounds.is_empty());
591+
assert!(self.impl_trait_defs.is_empty());
592+
assert!(self.impl_trait_bounds.is_empty());
593593
let info = self.make_owner_info(item);
594594

595595
self.attrs = current_attrs;
@@ -891,7 +891,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
891891
} else {
892892
let lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span));
893893

894-
debug_assert_eq!(id.owner, self.current_hir_id_owner);
894+
assert_eq!(id.owner, self.current_hir_id_owner);
895895
let ret = self.arena.alloc_from_iter(lowered_attrs);
896896

897897
// this is possible if an item contained syntactical attribute,
@@ -915,10 +915,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
915915
}
916916

917917
fn alias_attrs(&mut self, id: HirId, target_id: HirId) {
918-
debug_assert_eq!(id.owner, self.current_hir_id_owner);
919-
debug_assert_eq!(target_id.owner, self.current_hir_id_owner);
918+
assert_eq!(id.owner, self.current_hir_id_owner);
919+
assert_eq!(target_id.owner, self.current_hir_id_owner);
920920
if let Some(&a) = self.attrs.get(&target_id.local_id) {
921-
debug_assert!(!a.is_empty());
921+
assert!(!a.is_empty());
922922
self.attrs.insert(id.local_id, a);
923923
}
924924
}
@@ -1397,7 +1397,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13971397
let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
13981398
self.resolver.get_lifetime_res(t.id)
13991399
{
1400-
debug_assert_eq!(start.plus(1), end);
1400+
assert_eq!(start.plus(1), end);
14011401
start
14021402
} else {
14031403
self.next_node_id()
@@ -1805,16 +1805,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18051805
let res = match res {
18061806
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
18071807
LifetimeRes::Fresh { param, .. } => {
1808-
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1808+
assert_eq!(ident.name, kw::UnderscoreLifetime);
18091809
let param = self.local_def_id(param);
18101810
hir::LifetimeKind::Param(param)
18111811
}
18121812
LifetimeRes::Infer => {
1813-
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1813+
assert_eq!(ident.name, kw::UnderscoreLifetime);
18141814
hir::LifetimeKind::Infer
18151815
}
18161816
LifetimeRes::Static { .. } => {
1817-
debug_assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
1817+
assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
18181818
hir::LifetimeKind::Static
18191819
}
18201820
LifetimeRes::Error => hir::LifetimeKind::Error,
@@ -2244,7 +2244,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22442244
) -> hir::Stmt<'hir> {
22452245
let hir_id = self.next_id();
22462246
if let Some(a) = attrs {
2247-
debug_assert!(!a.is_empty());
2247+
assert!(!a.is_empty());
22482248
self.attrs.insert(hir_id.local_id, a);
22492249
}
22502250
let local = hir::LetStmt {

0 commit comments

Comments
 (0)