mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
internal: Remove CfgExpander
This commit is contained in:
parent
0dd2c0d8d3
commit
7b36a73351
2 changed files with 100 additions and 77 deletions
|
@ -18,7 +18,6 @@ use triomphe::Arc;
|
||||||
use crate::{
|
use crate::{
|
||||||
builtin_type::{BuiltinInt, BuiltinUint},
|
builtin_type::{BuiltinInt, BuiltinUint},
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
expander::CfgExpander,
|
|
||||||
item_tree::{AttrOwner, Field, FieldAstId, Fields, ItemTree, ModItem, RawVisibilityId},
|
item_tree::{AttrOwner, Field, FieldAstId, Fields, ItemTree, ModItem, RawVisibilityId},
|
||||||
lang_item::LangItem,
|
lang_item::LangItem,
|
||||||
lower::LowerCtx,
|
lower::LowerCtx,
|
||||||
|
@ -29,8 +28,8 @@ use crate::{
|
||||||
tt::{Delimiter, DelimiterKind, Leaf, Subtree, TokenTree},
|
tt::{Delimiter, DelimiterKind, Leaf, Subtree, TokenTree},
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
visibility::RawVisibility,
|
visibility::RawVisibility,
|
||||||
EnumId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StructId, UnionId,
|
EnumId, EnumLoc, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StructId,
|
||||||
VariantId,
|
UnionId, VariantId,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Note that we use `StructData` for unions as well!
|
/// Note that we use `StructData` for unions as well!
|
||||||
|
@ -76,6 +75,7 @@ pub struct EnumData {
|
||||||
pub struct EnumVariantData {
|
pub struct EnumVariantData {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
pub variant_data: Arc<VariantData>,
|
pub variant_data: Arc<VariantData>,
|
||||||
|
pub tree_id: la_arena::Idx<crate::item_tree::Variant>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
@ -326,6 +326,7 @@ impl EnumData {
|
||||||
variants.alloc(EnumVariantData {
|
variants.alloc(EnumVariantData {
|
||||||
name: var.name.clone(),
|
name: var.name.clone(),
|
||||||
variant_data: Arc::new(var_data),
|
variant_data: Arc::new(var_data),
|
||||||
|
tree_id,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
diagnostics.push(DefDiagnostic::unconfigured_code(
|
diagnostics.push(DefDiagnostic::unconfigured_code(
|
||||||
|
@ -368,9 +369,10 @@ impl HasChildSource<LocalEnumVariantId> for EnumId {
|
||||||
&self,
|
&self,
|
||||||
db: &dyn DefDatabase,
|
db: &dyn DefDatabase,
|
||||||
) -> InFile<ArenaMap<LocalEnumVariantId, Self::Value>> {
|
) -> InFile<ArenaMap<LocalEnumVariantId, Self::Value>> {
|
||||||
let src = self.lookup(db).source(db);
|
let loc = &self.lookup(db);
|
||||||
|
let src = loc.source(db);
|
||||||
let mut trace = Trace::new_for_map();
|
let mut trace = Trace::new_for_map();
|
||||||
lower_enum(db, &mut trace, &src, self.lookup(db).container);
|
lower_enum(db, &mut trace, &src, loc);
|
||||||
src.with_value(trace.into_map())
|
src.with_value(trace.into_map())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -379,31 +381,58 @@ fn lower_enum(
|
||||||
db: &dyn DefDatabase,
|
db: &dyn DefDatabase,
|
||||||
trace: &mut Trace<EnumVariantData, ast::Variant>,
|
trace: &mut Trace<EnumVariantData, ast::Variant>,
|
||||||
ast: &InFile<ast::Enum>,
|
ast: &InFile<ast::Enum>,
|
||||||
module_id: ModuleId,
|
loc: &EnumLoc,
|
||||||
) {
|
) {
|
||||||
let expander = CfgExpander::new(db, ast.file_id, module_id.krate);
|
let item_tree = loc.id.item_tree(db);
|
||||||
|
let krate = loc.container.krate;
|
||||||
|
|
||||||
|
let item_tree_variants = item_tree[loc.id.value].variants.clone();
|
||||||
|
|
||||||
|
let cfg_options = &db.crate_graph()[krate].cfg_options;
|
||||||
let variants = ast
|
let variants = ast
|
||||||
.value
|
.value
|
||||||
.variant_list()
|
.variant_list()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|it| it.variants())
|
.flat_map(|it| it.variants())
|
||||||
.filter(|var| expander.is_cfg_enabled(db, var));
|
.zip(item_tree_variants)
|
||||||
for var in variants {
|
.filter(|&(_, item_tree_id)| {
|
||||||
|
item_tree.attrs(db, krate, item_tree_id.into()).is_cfg_enabled(cfg_options)
|
||||||
|
});
|
||||||
|
for (var, item_tree_id) in variants {
|
||||||
trace.alloc(
|
trace.alloc(
|
||||||
|| var.clone(),
|
|| var.clone(),
|
||||||
|| EnumVariantData {
|
|| EnumVariantData {
|
||||||
name: var.name().map_or_else(Name::missing, |it| it.as_name()),
|
name: var.name().map_or_else(Name::missing, |it| it.as_name()),
|
||||||
variant_data: Arc::new(VariantData::new(db, ast.with_value(var.kind()), module_id)),
|
variant_data: Arc::new(VariantData::new(
|
||||||
|
db,
|
||||||
|
ast.with_value(var.kind()),
|
||||||
|
loc.container,
|
||||||
|
&item_tree,
|
||||||
|
item_tree_id,
|
||||||
|
)),
|
||||||
|
tree_id: item_tree_id,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VariantData {
|
impl VariantData {
|
||||||
fn new(db: &dyn DefDatabase, flavor: InFile<ast::StructKind>, module_id: ModuleId) -> Self {
|
fn new(
|
||||||
let mut expander = CfgExpander::new(db, flavor.file_id, module_id.krate);
|
db: &dyn DefDatabase,
|
||||||
|
flavor: InFile<ast::StructKind>,
|
||||||
|
module_id: ModuleId,
|
||||||
|
item_tree: &ItemTree,
|
||||||
|
variant: la_arena::Idx<crate::item_tree::Variant>,
|
||||||
|
) -> Self {
|
||||||
let mut trace = Trace::new_for_arena();
|
let mut trace = Trace::new_for_arena();
|
||||||
match lower_struct(db, &mut expander, &mut trace, &flavor) {
|
match lower_struct(
|
||||||
|
db,
|
||||||
|
&mut trace,
|
||||||
|
&flavor,
|
||||||
|
module_id.krate,
|
||||||
|
item_tree,
|
||||||
|
&item_tree[variant].fields,
|
||||||
|
) {
|
||||||
StructKind::Tuple => VariantData::Tuple(trace.into_arena()),
|
StructKind::Tuple => VariantData::Tuple(trace.into_arena()),
|
||||||
StructKind::Record => VariantData::Record(trace.into_arena()),
|
StructKind::Record => VariantData::Record(trace.into_arena()),
|
||||||
StructKind::Unit => VariantData::Unit,
|
StructKind::Unit => VariantData::Unit,
|
||||||
|
@ -435,28 +464,43 @@ impl HasChildSource<LocalFieldId> for VariantId {
|
||||||
type Value = Either<ast::TupleField, ast::RecordField>;
|
type Value = Either<ast::TupleField, ast::RecordField>;
|
||||||
|
|
||||||
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<LocalFieldId, Self::Value>> {
|
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<LocalFieldId, Self::Value>> {
|
||||||
let (src, module_id) = match self {
|
let item_tree;
|
||||||
|
let (src, fields, container) = match *self {
|
||||||
VariantId::EnumVariantId(it) => {
|
VariantId::EnumVariantId(it) => {
|
||||||
// I don't really like the fact that we call into parent source
|
// I don't really like the fact that we call into parent source
|
||||||
// here, this might add to more queries then necessary.
|
// here, this might add to more queries then necessary.
|
||||||
|
let lookup = it.parent.lookup(db);
|
||||||
|
item_tree = lookup.id.item_tree(db);
|
||||||
let src = it.parent.child_source(db);
|
let src = it.parent.child_source(db);
|
||||||
(src.map(|map| map[it.local_id].kind()), it.parent.lookup(db).container)
|
let tree_id = db.enum_data(it.parent).variants[it.local_id].tree_id;
|
||||||
|
let fields = &item_tree[tree_id].fields;
|
||||||
|
(src.map(|map| map[it.local_id].kind()), fields, lookup.container)
|
||||||
}
|
}
|
||||||
VariantId::StructId(it) => {
|
VariantId::StructId(it) => {
|
||||||
(it.lookup(db).source(db).map(|it| it.kind()), it.lookup(db).container)
|
let lookup = it.lookup(db);
|
||||||
|
item_tree = lookup.id.item_tree(db);
|
||||||
|
(
|
||||||
|
lookup.source(db).map(|it| it.kind()),
|
||||||
|
&item_tree[lookup.id.value].fields,
|
||||||
|
lookup.container,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
VariantId::UnionId(it) => {
|
||||||
|
let lookup = it.lookup(db);
|
||||||
|
item_tree = lookup.id.item_tree(db);
|
||||||
|
(
|
||||||
|
lookup.source(db).map(|it| {
|
||||||
|
it.record_field_list()
|
||||||
|
.map(ast::StructKind::Record)
|
||||||
|
.unwrap_or(ast::StructKind::Unit)
|
||||||
|
}),
|
||||||
|
&item_tree[lookup.id.value].fields,
|
||||||
|
lookup.container,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
VariantId::UnionId(it) => (
|
|
||||||
it.lookup(db).source(db).map(|it| {
|
|
||||||
it.record_field_list()
|
|
||||||
.map(ast::StructKind::Record)
|
|
||||||
.unwrap_or(ast::StructKind::Unit)
|
|
||||||
}),
|
|
||||||
it.lookup(db).container,
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
let mut expander = CfgExpander::new(db, src.file_id, module_id.krate);
|
|
||||||
let mut trace = Trace::new_for_map();
|
let mut trace = Trace::new_for_map();
|
||||||
lower_struct(db, &mut expander, &mut trace, &src);
|
lower_struct(db, &mut trace, &src, container.krate, &item_tree, fields);
|
||||||
src.with_value(trace.into_map())
|
src.with_value(trace.into_map())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -470,16 +514,19 @@ pub enum StructKind {
|
||||||
|
|
||||||
fn lower_struct(
|
fn lower_struct(
|
||||||
db: &dyn DefDatabase,
|
db: &dyn DefDatabase,
|
||||||
expander: &mut CfgExpander,
|
|
||||||
trace: &mut Trace<FieldData, Either<ast::TupleField, ast::RecordField>>,
|
trace: &mut Trace<FieldData, Either<ast::TupleField, ast::RecordField>>,
|
||||||
ast: &InFile<ast::StructKind>,
|
ast: &InFile<ast::StructKind>,
|
||||||
|
krate: CrateId,
|
||||||
|
item_tree: &ItemTree,
|
||||||
|
fields: &Fields,
|
||||||
) -> StructKind {
|
) -> StructKind {
|
||||||
let ctx = LowerCtx::new(db, &expander.hygiene(), ast.file_id);
|
let ctx = LowerCtx::with_file_id(db, ast.file_id);
|
||||||
|
|
||||||
match &ast.value {
|
match (&ast.value, fields) {
|
||||||
ast::StructKind::Tuple(fl) => {
|
(ast::StructKind::Tuple(fl), Fields::Tuple(fields)) => {
|
||||||
for (i, fd) in fl.fields().enumerate() {
|
let cfg_options = &db.crate_graph()[krate].cfg_options;
|
||||||
if !expander.is_cfg_enabled(db, &fd) {
|
for ((i, fd), item_tree_id) in fl.fields().enumerate().zip(fields.clone()) {
|
||||||
|
if !item_tree.attrs(db, krate, item_tree_id.into()).is_cfg_enabled(cfg_options) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -494,9 +541,10 @@ fn lower_struct(
|
||||||
}
|
}
|
||||||
StructKind::Tuple
|
StructKind::Tuple
|
||||||
}
|
}
|
||||||
ast::StructKind::Record(fl) => {
|
(ast::StructKind::Record(fl), Fields::Record(fields)) => {
|
||||||
for fd in fl.fields() {
|
let cfg_options = &db.crate_graph()[krate].cfg_options;
|
||||||
if !expander.is_cfg_enabled(db, &fd) {
|
for (fd, item_tree_id) in fl.fields().zip(fields.clone()) {
|
||||||
|
if !item_tree.attrs(db, krate, item_tree_id.into()).is_cfg_enabled(cfg_options) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -511,7 +559,7 @@ fn lower_struct(
|
||||||
}
|
}
|
||||||
StructKind::Record
|
StructKind::Record
|
||||||
}
|
}
|
||||||
ast::StructKind::Unit => StructKind::Unit,
|
_ => StructKind::Unit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,18 +15,11 @@ use crate::{
|
||||||
MacroId, ModuleId,
|
MacroId, ModuleId,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A subset of Expander that only deals with cfg attributes. We only need it to
|
|
||||||
/// avoid cyclic queries in crate def map during enum processing.
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct CfgExpander {
|
pub struct Expander {
|
||||||
cfg_options: CfgOptions,
|
cfg_options: CfgOptions,
|
||||||
hygiene: Hygiene,
|
hygiene: Hygiene,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Expander {
|
|
||||||
cfg_expander: CfgExpander,
|
|
||||||
pub(crate) current_file_id: HirFileId,
|
pub(crate) current_file_id: HirFileId,
|
||||||
pub(crate) module: ModuleId,
|
pub(crate) module: ModuleId,
|
||||||
/// `recursion_depth == usize::MAX` indicates that the recursion limit has been reached.
|
/// `recursion_depth == usize::MAX` indicates that the recursion limit has been reached.
|
||||||
|
@ -34,41 +27,23 @@ pub struct Expander {
|
||||||
recursion_limit: Limit,
|
recursion_limit: Limit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CfgExpander {
|
|
||||||
pub(crate) fn new(
|
|
||||||
db: &dyn DefDatabase,
|
|
||||||
current_file_id: HirFileId,
|
|
||||||
krate: CrateId,
|
|
||||||
) -> CfgExpander {
|
|
||||||
let hygiene = Hygiene::new(db.upcast(), current_file_id);
|
|
||||||
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
|
|
||||||
CfgExpander { cfg_options, hygiene, krate }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs {
|
|
||||||
Attrs::filter(db, self.krate, RawAttrs::new(db.upcast(), owner, &self.hygiene))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn is_cfg_enabled(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> bool {
|
|
||||||
let attrs = self.parse_attrs(db, owner);
|
|
||||||
attrs.is_cfg_enabled(&self.cfg_options)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn hygiene(&self) -> &Hygiene {
|
|
||||||
&self.hygiene
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Expander {
|
impl Expander {
|
||||||
pub fn new(db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
|
pub fn new(db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
|
||||||
let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
|
|
||||||
let recursion_limit = db.recursion_limit(module.krate);
|
let recursion_limit = db.recursion_limit(module.krate);
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
let recursion_limit = Limit::new(recursion_limit as usize);
|
let recursion_limit = Limit::new(recursion_limit as usize);
|
||||||
// Without this, `body::tests::your_stack_belongs_to_me` stack-overflows in debug
|
// Without this, `body::tests::your_stack_belongs_to_me` stack-overflows in debug
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
let recursion_limit = Limit::new(std::cmp::min(32, recursion_limit as usize));
|
let recursion_limit = Limit::new(std::cmp::min(32, recursion_limit as usize));
|
||||||
Expander { cfg_expander, current_file_id, module, recursion_depth: 0, recursion_limit }
|
Expander {
|
||||||
|
current_file_id,
|
||||||
|
module,
|
||||||
|
recursion_depth: 0,
|
||||||
|
recursion_limit,
|
||||||
|
cfg_options: db.crate_graph()[module.krate].cfg_options.clone(),
|
||||||
|
hygiene: Hygiene::new(db.upcast(), current_file_id),
|
||||||
|
krate: module.krate,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_expand<T: ast::AstNode>(
|
pub fn enter_expand<T: ast::AstNode>(
|
||||||
|
@ -120,7 +95,7 @@ impl Expander {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
|
pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
|
||||||
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id);
|
self.hygiene = Hygiene::new(db.upcast(), mark.file_id);
|
||||||
self.current_file_id = mark.file_id;
|
self.current_file_id = mark.file_id;
|
||||||
if self.recursion_depth == u32::MAX {
|
if self.recursion_depth == u32::MAX {
|
||||||
// Recursion limit has been reached somewhere in the macro expansion tree. Reset the
|
// Recursion limit has been reached somewhere in the macro expansion tree. Reset the
|
||||||
|
@ -135,7 +110,7 @@ impl Expander {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ctx<'a>(&self, db: &'a dyn DefDatabase) -> LowerCtx<'a> {
|
pub fn ctx<'a>(&self, db: &'a dyn DefDatabase) -> LowerCtx<'a> {
|
||||||
LowerCtx::new(db, &self.cfg_expander.hygiene, self.current_file_id)
|
LowerCtx::new(db, &self.hygiene, self.current_file_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn to_source<T>(&self, value: T) -> InFile<T> {
|
pub(crate) fn to_source<T>(&self, value: T) -> InFile<T> {
|
||||||
|
@ -143,11 +118,11 @@ impl Expander {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs {
|
pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs {
|
||||||
self.cfg_expander.parse_attrs(db, owner)
|
Attrs::filter(db, self.krate, RawAttrs::new(db.upcast(), owner, &self.hygiene))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn cfg_options(&self) -> &CfgOptions {
|
pub(crate) fn cfg_options(&self) -> &CfgOptions {
|
||||||
&self.cfg_expander.cfg_options
|
&self.cfg_options
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_file_id(&self) -> HirFileId {
|
pub fn current_file_id(&self) -> HirFileId {
|
||||||
|
@ -155,7 +130,7 @@ impl Expander {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> {
|
pub(crate) fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> {
|
||||||
let ctx = LowerCtx::new(db, &self.cfg_expander.hygiene, self.current_file_id);
|
let ctx = LowerCtx::new(db, &self.hygiene, self.current_file_id);
|
||||||
Path::from_src(path, &ctx)
|
Path::from_src(path, &ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +169,7 @@ impl Expander {
|
||||||
let parse = value.cast::<T>()?;
|
let parse = value.cast::<T>()?;
|
||||||
|
|
||||||
self.recursion_depth += 1;
|
self.recursion_depth += 1;
|
||||||
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
|
self.hygiene = Hygiene::new(db.upcast(), file_id);
|
||||||
let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
|
let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
|
||||||
let mark =
|
let mark =
|
||||||
Mark { file_id: old_file_id, bomb: DropBomb::new("expansion mark dropped") };
|
Mark { file_id: old_file_id, bomb: DropBomb::new("expansion mark dropped") };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue