Skip to content

Commit 27c3ed9

Browse files
committed
Auto merge of rust-lang#16434 - Veykril:things, r=Veykril
internal: Restructure and cleanup hir-expand a bit
2 parents 4a23744 + 8a5829c commit 27c3ed9

File tree

62 files changed

+749
-723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+749
-723
lines changed

crates/base-db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
#![warn(rust_2018_idioms, unused_lifetimes)]
44

5-
mod input;
65
mod change;
6+
mod input;
77

88
use std::panic;
99

crates/cfg/src/cfg_expr.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,6 @@ pub enum CfgAtom {
1818
KeyValue { key: SmolStr, value: SmolStr },
1919
}
2020

21-
impl CfgAtom {
22-
/// Returns `true` when the atom comes from the target specification.
23-
///
24-
/// If this returns `true`, then changing this atom requires changing the compilation target. If
25-
/// it returns `false`, the atom might come from a build script or the build system.
26-
pub fn is_target_defined(&self) -> bool {
27-
match self {
28-
CfgAtom::Flag(flag) => matches!(&**flag, "unix" | "windows"),
29-
CfgAtom::KeyValue { key, value: _ } => matches!(
30-
&**key,
31-
"target_arch"
32-
| "target_os"
33-
| "target_env"
34-
| "target_family"
35-
| "target_endian"
36-
| "target_pointer_width"
37-
| "target_vendor" // NOTE: `target_feature` is left out since it can be configured via `-Ctarget-feature`
38-
),
39-
}
40-
}
41-
}
42-
4321
impl fmt::Display for CfgAtom {
4422
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4523
match self {

crates/cfg/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,9 @@ impl CfgDiff {
131131
/// of both.
132132
pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> {
133133
let mut occupied = FxHashSet::default();
134-
for item in enable.iter().chain(disable.iter()) {
135-
if !occupied.insert(item) {
136-
// was present
137-
return None;
138-
}
134+
if enable.iter().chain(disable.iter()).any(|item| !occupied.insert(item)) {
135+
// was present
136+
return None;
139137
}
140138

141139
Some(CfgDiff { enable, disable })

crates/hir-def/src/attr.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::{
3232
VariantId,
3333
};
3434

35+
/// Desugared attributes of an item post `cfg_attr` expansion.
3536
#[derive(Default, Debug, Clone, PartialEq, Eq)]
3637
pub struct Attrs(RawAttrs);
3738

@@ -228,7 +229,6 @@ pub enum DocAtom {
228229
KeyValue { key: SmolStr, value: SmolStr },
229230
}
230231

231-
// Adapted from `CfgExpr` parsing code
232232
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
233233
pub enum DocExpr {
234234
Invalid,
@@ -448,10 +448,7 @@ impl AttrsWithOwner {
448448
let map = db.fields_attrs_source_map(id.parent);
449449
let file_id = id.parent.file_id(db);
450450
let root = db.parse_or_expand(file_id);
451-
let owner = match &map[id.local_id] {
452-
Either::Left(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
453-
Either::Right(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
454-
};
451+
let owner = ast::AnyHasAttrs::new(map[id.local_id].to_node(&root));
455452
InFile::new(file_id, owner)
456453
}
457454
AttrDefId::AdtId(adt) => match adt {
@@ -634,7 +631,7 @@ fn attrs_from_item_tree_assoc<'db, N: ItemTreeModItemNode>(
634631
pub(crate) fn fields_attrs_source_map(
635632
db: &dyn DefDatabase,
636633
def: VariantId,
637-
) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>> {
634+
) -> Arc<ArenaMap<LocalFieldId, AstPtr<Either<ast::TupleField, ast::RecordField>>>> {
638635
let mut res = ArenaMap::default();
639636
let child_source = def.child_source(db);
640637

@@ -643,7 +640,7 @@ pub(crate) fn fields_attrs_source_map(
643640
idx,
644641
variant
645642
.as_ref()
646-
.either(|l| Either::Left(AstPtr::new(l)), |r| Either::Right(AstPtr::new(r))),
643+
.either(|l| AstPtr::new(l).wrap_left(), |r| AstPtr::new(r).wrap_right()),
647644
);
648645
}
649646

crates/hir-def/src/body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Defines `Body`: a lowered representation of bodies of functions, statics and
22
//! consts.
33
mod lower;
4+
mod pretty;
5+
pub mod scope;
46
#[cfg(test)]
57
mod tests;
6-
pub mod scope;
7-
mod pretty;
88

99
use std::ops::Index;
1010

crates/hir-def/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
194194
fn fields_attrs_source_map(
195195
&self,
196196
def: VariantId,
197-
) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>>;
197+
) -> Arc<ArenaMap<LocalFieldId, AstPtr<Either<ast::TupleField, ast::RecordField>>>>;
198198

199199
#[salsa::invoke(AttrsWithOwner::attrs_query)]
200200
fn attrs(&self, def: AttrDefId) -> Attrs;

crates/hir-def/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//!
1313
//! See also a neighboring `body` module.
1414
15-
pub mod type_ref;
1615
pub mod format_args;
16+
pub mod type_ref;
1717

1818
use std::fmt;
1919

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/macro_expansion_tests/mbe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Tests specific to declarative macros, aka macros by example. This covers
22
//! both stable `macro_rules!` macros as well as unstable `macro` macros.
33
4-
mod tt_conversion;
54
mod matching;
65
mod meta_syntax;
76
mod metavar_expr;
87
mod regression;
8+
mod tt_conversion;
99

1010
use expect_test::expect;
1111

crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
//! write unit-tests (in fact, we used to do that), but that makes tests brittle
1010
//! and harder to understand.
1111
12-
mod mbe;
13-
mod builtin_fn_macro;
1412
mod builtin_derive_macro;
13+
mod builtin_fn_macro;
14+
mod mbe;
1515
mod proc_macros;
1616

1717
use std::{iter, ops::Range, sync};

crates/hir-def/src/nameres.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
//! the result
4949
5050
pub mod attr_resolution;
51-
pub mod proc_macro;
52-
pub mod diagnostics;
5351
mod collector;
52+
pub mod diagnostics;
5453
mod mod_resolution;
5554
mod path_resolution;
55+
pub mod proc_macro;
5656

5757
#[cfg(test)]
5858
mod tests;

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+
}

0 commit comments

Comments
 (0)