Skip to content

Commit 5a34341

Browse files
committed
Add some size assertions
1 parent d8ef6c2 commit 5a34341

File tree

13 files changed

+194
-198
lines changed

13 files changed

+194
-198
lines changed

crates/hir-def/src/lib.rs

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ extern crate ra_ap_rustc_abi as rustc_abi;
2525
pub mod db;
2626

2727
pub mod attr;
28-
pub mod path;
2928
pub mod builtin_type;
30-
pub mod per_ns;
3129
pub mod item_scope;
30+
pub mod path;
31+
pub mod per_ns;
3232

33-
pub mod lower;
3433
pub mod expander;
34+
pub mod lower;
3535

3636
pub mod dyn_map;
3737

@@ -46,24 +46,24 @@ pub use self::hir::type_ref;
4646
pub mod body;
4747
pub mod resolver;
4848

49-
mod trace;
5049
pub mod nameres;
50+
mod trace;
5151

52-
pub mod src;
5352
pub mod child_by_source;
53+
pub mod src;
5454

55-
pub mod visibility;
5655
pub mod find_path;
5756
pub mod import_map;
57+
pub mod visibility;
5858

5959
pub use rustc_abi as layout;
6060
use triomphe::Arc;
6161

62-
#[cfg(test)]
63-
mod test_db;
6462
#[cfg(test)]
6563
mod macro_expansion_tests;
6664
mod pretty;
65+
#[cfg(test)]
66+
mod test_db;
6767

6868
use std::{
6969
hash::{Hash, Hasher},
@@ -73,7 +73,6 @@ use std::{
7373
use base_db::{impl_intern_key, salsa, CrateId, Edition};
7474
use hir_expand::{
7575
ast_id_map::{AstIdNode, FileAstId},
76-
attrs::{Attr, AttrId, AttrInput},
7776
builtin_attr_macro::BuiltinAttrExpander,
7877
builtin_derive_macro::BuiltinDeriveExpander,
7978
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
@@ -1274,60 +1273,6 @@ fn macro_call_as_call_id_with_eager(
12741273
Ok(res)
12751274
}
12761275

1277-
fn derive_macro_as_call_id(
1278-
db: &dyn DefDatabase,
1279-
item_attr: &AstIdWithPath<ast::Adt>,
1280-
derive_attr_index: AttrId,
1281-
derive_pos: u32,
1282-
call_site: Span,
1283-
krate: CrateId,
1284-
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
1285-
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
1286-
let (macro_id, def_id) = resolver(item_attr.path.clone())
1287-
.filter(|(_, def_id)| def_id.is_derive())
1288-
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
1289-
let call_id = def_id.as_lazy_macro(
1290-
db.upcast(),
1291-
krate,
1292-
MacroCallKind::Derive {
1293-
ast_id: item_attr.ast_id,
1294-
derive_index: derive_pos,
1295-
derive_attr_index,
1296-
},
1297-
call_site,
1298-
);
1299-
Ok((macro_id, def_id, call_id))
1300-
}
1301-
1302-
fn attr_macro_as_call_id(
1303-
db: &dyn DefDatabase,
1304-
item_attr: &AstIdWithPath<ast::Item>,
1305-
macro_attr: &Attr,
1306-
krate: CrateId,
1307-
def: MacroDefId,
1308-
) -> MacroCallId {
1309-
let arg = match macro_attr.input.as_deref() {
1310-
Some(AttrInput::TokenTree(tt)) => {
1311-
let mut tt = tt.as_ref().clone();
1312-
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
1313-
Some(tt)
1314-
}
1315-
1316-
_ => None,
1317-
};
1318-
1319-
def.as_lazy_macro(
1320-
db.upcast(),
1321-
krate,
1322-
MacroCallKind::Attr {
1323-
ast_id: item_attr.ast_id,
1324-
attr_args: arg.map(Arc::new),
1325-
invoc_attr_index: macro_attr.id,
1326-
},
1327-
macro_attr.span,
1328-
)
1329-
}
1330-
13311276
#[derive(Debug)]
13321277
pub struct UnresolvedMacro {
13331278
pub path: hir_expand::mod_path::ModPath,

crates/hir-def/src/nameres/attr_resolution.rs

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
//! Post-nameres attribute resolution.
22
3-
use hir_expand::{attrs::Attr, MacroCallId};
3+
use base_db::CrateId;
4+
use hir_expand::{
5+
attrs::{Attr, AttrId, AttrInput},
6+
MacroCallId, MacroCallKind, MacroDefId,
7+
};
8+
use span::Span;
49
use syntax::{ast, SmolStr};
10+
use triomphe::Arc;
511

612
use crate::{
713
attr::builtin::{find_builtin_attr_idx, TOOL_MODULES},
8-
attr_macro_as_call_id,
914
db::DefDatabase,
1015
item_scope::BuiltinShadowMode,
1116
nameres::path_resolution::ResolveMode,
12-
path::{ModPath, PathKind},
13-
AstIdWithPath, LocalModuleId, UnresolvedMacro,
17+
path::{self, ModPath, PathKind},
18+
AstIdWithPath, LocalModuleId, MacroId, UnresolvedMacro,
1419
};
1520

1621
use super::{DefMap, MacroSubNs};
@@ -93,3 +98,57 @@ impl DefMap {
9398
false
9499
}
95100
}
101+
102+
pub(super) fn attr_macro_as_call_id(
103+
db: &dyn DefDatabase,
104+
item_attr: &AstIdWithPath<ast::Item>,
105+
macro_attr: &Attr,
106+
krate: CrateId,
107+
def: MacroDefId,
108+
) -> MacroCallId {
109+
let arg = match macro_attr.input.as_deref() {
110+
Some(AttrInput::TokenTree(tt)) => {
111+
let mut tt = tt.as_ref().clone();
112+
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
113+
Some(tt)
114+
}
115+
116+
_ => None,
117+
};
118+
119+
def.as_lazy_macro(
120+
db.upcast(),
121+
krate,
122+
MacroCallKind::Attr {
123+
ast_id: item_attr.ast_id,
124+
attr_args: arg.map(Arc::new),
125+
invoc_attr_index: macro_attr.id,
126+
},
127+
macro_attr.span,
128+
)
129+
}
130+
131+
pub(super) fn derive_macro_as_call_id(
132+
db: &dyn DefDatabase,
133+
item_attr: &AstIdWithPath<ast::Adt>,
134+
derive_attr_index: AttrId,
135+
derive_pos: u32,
136+
call_site: Span,
137+
krate: CrateId,
138+
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
139+
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
140+
let (macro_id, def_id) = resolver(item_attr.path.clone())
141+
.filter(|(_, def_id)| def_id.is_derive())
142+
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
143+
let call_id = def_id.as_lazy_macro(
144+
db.upcast(),
145+
krate,
146+
MacroCallKind::Derive {
147+
ast_id: item_attr.ast_id,
148+
derive_index: derive_pos,
149+
derive_attr_index,
150+
},
151+
call_site,
152+
);
153+
Ok((macro_id, def_id, call_id))
154+
}

crates/hir-def/src/nameres/collector.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ use triomphe::Arc;
3030

3131
use crate::{
3232
attr::Attrs,
33-
attr_macro_as_call_id,
3433
db::DefDatabase,
35-
derive_macro_as_call_id,
3634
item_scope::{ImportId, ImportOrExternCrate, ImportType, PerNsGlobImports},
3735
item_tree::{
3836
self, ExternCrate, Fields, FileItemTreeId, ImportKind, ItemTree, ItemTreeId,
3937
ItemTreeModItemNode, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId,
4038
},
4139
macro_call_as_call_id, macro_call_as_call_id_with_eager,
4240
nameres::{
41+
attr_resolution::{attr_macro_as_call_id, derive_macro_as_call_id},
4342
diagnostics::DefDiagnostic,
4443
mod_resolution::ModDir,
4544
path_resolution::ReachedFixedPoint,
@@ -1245,7 +1244,9 @@ impl DefCollector<'_> {
12451244
MacroDefId { kind: MacroDefKind::BuiltInAttr(expander, _),.. }
12461245
if expander.is_derive()
12471246
) {
1248-
// Resolved to `#[derive]`
1247+
// Resolved to `#[derive]`, we don't actually expand this attribute like
1248+
// normal (as that would just be an identity expansion with extra output)
1249+
// Instead we treat derive attributes special and apply them separately.
12491250

12501251
let item_tree = tree.item_tree(self.db);
12511252
let ast_adt_id: FileAstId<ast::Adt> = match *mod_item {
@@ -1284,7 +1285,8 @@ impl DefCollector<'_> {
12841285
}
12851286

12861287
// We treat the #[derive] macro as an attribute call, but we do not resolve it for nameres collection.
1287-
// This is just a trick to be able to resolve the input to derives as proper paths.
1288+
// This is just a trick to be able to resolve the input to derives
1289+
// as proper paths in `Semantics`.
12881290
// Check the comment in [`builtin_attr_macro`].
12891291
let call_id = attr_macro_as_call_id(
12901292
self.db,

crates/hir-def/src/visibility.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::iter;
44

55
use hir_expand::{span_map::SpanMapRef, InFile};
66
use la_arena::ArenaMap;
7+
use stdx::assert_eq_size;
78
use syntax::ast;
89
use triomphe::Arc;
910

@@ -24,6 +25,7 @@ pub enum RawVisibility {
2425
/// `pub`.
2526
Public,
2627
}
28+
assert_eq_size!(RawVisibility, 48);
2729

2830
impl RawVisibility {
2931
pub(crate) const fn private() -> RawVisibility {

crates/hir-expand/src/builtin_attr_macro.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ impl BuiltinAttrExpander {
4848

4949
register_builtin! { expand:
5050
(bench, Bench) => dummy_attr_expand,
51+
(cfg, Cfg) => dummy_attr_expand,
52+
(cfg_attr, CfgAttr) => dummy_attr_expand,
5153
(cfg_accessible, CfgAccessible) => dummy_attr_expand,
5254
(cfg_eval, CfgEval) => dummy_attr_expand,
53-
(derive, Derive) => derive_attr_expand,
55+
(derive, Derive) => derive_expand,
5456
// derive const is equivalent to derive for our proposes.
55-
(derive_const, DeriveConst) => derive_attr_expand,
57+
(derive_const, DeriveConst) => derive_expand,
5658
(global_allocator, GlobalAllocator) => dummy_attr_expand,
5759
(test, Test) => dummy_attr_expand,
5860
(test_case, TestCase) => dummy_attr_expand
@@ -91,7 +93,7 @@ fn dummy_attr_expand(
9193
/// always resolve as a derive without nameres recollecting them.
9294
/// So this hacky approach is a lot more friendly for us, though it does require a bit of support in
9395
/// [`hir::Semantics`] to make this work.
94-
fn derive_attr_expand(
96+
fn derive_expand(
9597
db: &dyn ExpandDatabase,
9698
id: MacroCallId,
9799
tt: &tt::Subtree,

crates/hir-expand/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod span_map;
2626
mod fixup;
2727

2828
use attrs::collect_attrs;
29+
use stdx::assert_eq_size;
2930
use triomphe::Arc;
3031

3132
use std::{fmt, hash::Hash};
@@ -175,6 +176,7 @@ pub struct MacroCallLoc {
175176
pub kind: MacroCallKind,
176177
pub call_site: Span,
177178
}
179+
assert_eq_size!(MacroCallLoc, 104);
178180

179181
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
180182
pub struct MacroDefId {
@@ -185,6 +187,7 @@ pub struct MacroDefId {
185187
pub allow_internal_unsafe: bool,
186188
pub span: Span,
187189
}
190+
assert_eq_size!(MacroDefId, 44);
188191

189192
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
190193
pub enum MacroDefKind {

crates/hir-expand/src/mod_path.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ use crate::{
1515
use base_db::CrateId;
1616
use smallvec::SmallVec;
1717
use span::SyntaxContextId;
18+
use stdx::assert_eq_size;
1819
use syntax::{ast, AstNode};
1920

2021
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2122
pub struct ModPath {
2223
pub kind: PathKind,
2324
segments: SmallVec<[Name; 1]>,
2425
}
26+
assert_eq_size!(ModPath, 40);
2527

2628
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2729
pub struct UnescapedModPath<'a>(&'a ModPath);

crates/hir-expand/src/name.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::fmt;
44

5+
use stdx::assert_eq_size;
56
use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};
67

78
/// `Name` is a wrapper around string, which is used in hir for both references
@@ -13,6 +14,7 @@ use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};
1314
/// name without "r#".
1415
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1516
pub struct Name(Repr);
17+
assert_eq_size!(Name, 24);
1618

1719
/// Wrapper of `Name` to print the name without "r#" even when it is a raw identifier.
1820
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]

0 commit comments

Comments
 (0)