Skip to content

Commit

Permalink
Add some size assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 26, 2024
1 parent d8ef6c2 commit 5a34341
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 198 deletions.
71 changes: 8 additions & 63 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ extern crate ra_ap_rustc_abi as rustc_abi;
pub mod db;

pub mod attr;
pub mod path;
pub mod builtin_type;
pub mod per_ns;
pub mod item_scope;
pub mod path;
pub mod per_ns;

pub mod lower;
pub mod expander;
pub mod lower;

pub mod dyn_map;

Expand All @@ -46,24 +46,24 @@ pub use self::hir::type_ref;
pub mod body;
pub mod resolver;

mod trace;
pub mod nameres;
mod trace;

pub mod src;
pub mod child_by_source;
pub mod src;

pub mod visibility;
pub mod find_path;
pub mod import_map;
pub mod visibility;

pub use rustc_abi as layout;
use triomphe::Arc;

#[cfg(test)]
mod test_db;
#[cfg(test)]
mod macro_expansion_tests;
mod pretty;
#[cfg(test)]
mod test_db;

use std::{
hash::{Hash, Hasher},
Expand All @@ -73,7 +73,6 @@ use std::{
use base_db::{impl_intern_key, salsa, CrateId, Edition};
use hir_expand::{
ast_id_map::{AstIdNode, FileAstId},
attrs::{Attr, AttrId, AttrInput},
builtin_attr_macro::BuiltinAttrExpander,
builtin_derive_macro::BuiltinDeriveExpander,
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
Expand Down Expand Up @@ -1274,60 +1273,6 @@ fn macro_call_as_call_id_with_eager(
Ok(res)
}

fn derive_macro_as_call_id(
db: &dyn DefDatabase,
item_attr: &AstIdWithPath<ast::Adt>,
derive_attr_index: AttrId,
derive_pos: u32,
call_site: Span,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
let (macro_id, def_id) = resolver(item_attr.path.clone())
.filter(|(_, def_id)| def_id.is_derive())
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
let call_id = def_id.as_lazy_macro(
db.upcast(),
krate,
MacroCallKind::Derive {
ast_id: item_attr.ast_id,
derive_index: derive_pos,
derive_attr_index,
},
call_site,
);
Ok((macro_id, def_id, call_id))
}

fn attr_macro_as_call_id(
db: &dyn DefDatabase,
item_attr: &AstIdWithPath<ast::Item>,
macro_attr: &Attr,
krate: CrateId,
def: MacroDefId,
) -> MacroCallId {
let arg = match macro_attr.input.as_deref() {
Some(AttrInput::TokenTree(tt)) => {
let mut tt = tt.as_ref().clone();
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
Some(tt)
}

_ => None,
};

def.as_lazy_macro(
db.upcast(),
krate,
MacroCallKind::Attr {
ast_id: item_attr.ast_id,
attr_args: arg.map(Arc::new),
invoc_attr_index: macro_attr.id,
},
macro_attr.span,
)
}

#[derive(Debug)]
pub struct UnresolvedMacro {
pub path: hir_expand::mod_path::ModPath,
Expand Down
67 changes: 63 additions & 4 deletions crates/hir-def/src/nameres/attr_resolution.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
//! Post-nameres attribute resolution.
use hir_expand::{attrs::Attr, MacroCallId};
use base_db::CrateId;
use hir_expand::{
attrs::{Attr, AttrId, AttrInput},
MacroCallId, MacroCallKind, MacroDefId,
};
use span::Span;
use syntax::{ast, SmolStr};
use triomphe::Arc;

use crate::{
attr::builtin::{find_builtin_attr_idx, TOOL_MODULES},
attr_macro_as_call_id,
db::DefDatabase,
item_scope::BuiltinShadowMode,
nameres::path_resolution::ResolveMode,
path::{ModPath, PathKind},
AstIdWithPath, LocalModuleId, UnresolvedMacro,
path::{self, ModPath, PathKind},
AstIdWithPath, LocalModuleId, MacroId, UnresolvedMacro,
};

use super::{DefMap, MacroSubNs};
Expand Down Expand Up @@ -93,3 +98,57 @@ impl DefMap {
false
}
}

pub(super) fn attr_macro_as_call_id(
db: &dyn DefDatabase,
item_attr: &AstIdWithPath<ast::Item>,
macro_attr: &Attr,
krate: CrateId,
def: MacroDefId,
) -> MacroCallId {
let arg = match macro_attr.input.as_deref() {
Some(AttrInput::TokenTree(tt)) => {
let mut tt = tt.as_ref().clone();
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
Some(tt)
}

_ => None,
};

def.as_lazy_macro(
db.upcast(),
krate,
MacroCallKind::Attr {
ast_id: item_attr.ast_id,
attr_args: arg.map(Arc::new),
invoc_attr_index: macro_attr.id,
},
macro_attr.span,
)
}

pub(super) fn derive_macro_as_call_id(
db: &dyn DefDatabase,
item_attr: &AstIdWithPath<ast::Adt>,
derive_attr_index: AttrId,
derive_pos: u32,
call_site: Span,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
let (macro_id, def_id) = resolver(item_attr.path.clone())
.filter(|(_, def_id)| def_id.is_derive())
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
let call_id = def_id.as_lazy_macro(
db.upcast(),
krate,
MacroCallKind::Derive {
ast_id: item_attr.ast_id,
derive_index: derive_pos,
derive_attr_index,
},
call_site,
);
Ok((macro_id, def_id, call_id))
}
10 changes: 6 additions & 4 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ use triomphe::Arc;

use crate::{
attr::Attrs,
attr_macro_as_call_id,
db::DefDatabase,
derive_macro_as_call_id,
item_scope::{ImportId, ImportOrExternCrate, ImportType, PerNsGlobImports},
item_tree::{
self, ExternCrate, Fields, FileItemTreeId, ImportKind, ItemTree, ItemTreeId,
ItemTreeModItemNode, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId,
},
macro_call_as_call_id, macro_call_as_call_id_with_eager,
nameres::{
attr_resolution::{attr_macro_as_call_id, derive_macro_as_call_id},
diagnostics::DefDiagnostic,
mod_resolution::ModDir,
path_resolution::ReachedFixedPoint,
Expand Down Expand Up @@ -1245,7 +1244,9 @@ impl DefCollector<'_> {
MacroDefId { kind: MacroDefKind::BuiltInAttr(expander, _),.. }
if expander.is_derive()
) {
// Resolved to `#[derive]`
// Resolved to `#[derive]`, we don't actually expand this attribute like
// normal (as that would just be an identity expansion with extra output)
// Instead we treat derive attributes special and apply them separately.

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

// We treat the #[derive] macro as an attribute call, but we do not resolve it for nameres collection.
// This is just a trick to be able to resolve the input to derives as proper paths.
// This is just a trick to be able to resolve the input to derives
// as proper paths in `Semantics`.
// Check the comment in [`builtin_attr_macro`].
let call_id = attr_macro_as_call_id(
self.db,
Expand Down
2 changes: 2 additions & 0 deletions crates/hir-def/src/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::iter;

use hir_expand::{span_map::SpanMapRef, InFile};
use la_arena::ArenaMap;
use stdx::assert_eq_size;
use syntax::ast;
use triomphe::Arc;

Expand All @@ -24,6 +25,7 @@ pub enum RawVisibility {
/// `pub`.
Public,
}
assert_eq_size!(RawVisibility, 48);

impl RawVisibility {
pub(crate) const fn private() -> RawVisibility {
Expand Down
8 changes: 5 additions & 3 deletions crates/hir-expand/src/builtin_attr_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ impl BuiltinAttrExpander {

register_builtin! { expand:
(bench, Bench) => dummy_attr_expand,
(cfg, Cfg) => dummy_attr_expand,
(cfg_attr, CfgAttr) => dummy_attr_expand,
(cfg_accessible, CfgAccessible) => dummy_attr_expand,
(cfg_eval, CfgEval) => dummy_attr_expand,
(derive, Derive) => derive_attr_expand,
(derive, Derive) => derive_expand,
// derive const is equivalent to derive for our proposes.
(derive_const, DeriveConst) => derive_attr_expand,
(derive_const, DeriveConst) => derive_expand,
(global_allocator, GlobalAllocator) => dummy_attr_expand,
(test, Test) => dummy_attr_expand,
(test_case, TestCase) => dummy_attr_expand
Expand Down Expand Up @@ -91,7 +93,7 @@ fn dummy_attr_expand(
/// always resolve as a derive without nameres recollecting them.
/// So this hacky approach is a lot more friendly for us, though it does require a bit of support in
/// [`hir::Semantics`] to make this work.
fn derive_attr_expand(
fn derive_expand(
db: &dyn ExpandDatabase,
id: MacroCallId,
tt: &tt::Subtree,
Expand Down
3 changes: 3 additions & 0 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod span_map;
mod fixup;

use attrs::collect_attrs;
use stdx::assert_eq_size;
use triomphe::Arc;

use std::{fmt, hash::Hash};
Expand Down Expand Up @@ -175,6 +176,7 @@ pub struct MacroCallLoc {
pub kind: MacroCallKind,
pub call_site: Span,
}
assert_eq_size!(MacroCallLoc, 104);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId {
Expand All @@ -185,6 +187,7 @@ pub struct MacroDefId {
pub allow_internal_unsafe: bool,
pub span: Span,
}
assert_eq_size!(MacroDefId, 44);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroDefKind {
Expand Down
2 changes: 2 additions & 0 deletions crates/hir-expand/src/mod_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ use crate::{
use base_db::CrateId;
use smallvec::SmallVec;
use span::SyntaxContextId;
use stdx::assert_eq_size;
use syntax::{ast, AstNode};

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

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UnescapedModPath<'a>(&'a ModPath);
Expand Down
2 changes: 2 additions & 0 deletions crates/hir-expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::fmt;

use stdx::assert_eq_size;
use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};

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

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

0 comments on commit 5a34341

Please sign in to comment.