Skip to content

Commit b3f4574

Browse files
committed
Auto merge of #15677 - Veykril:param-ast-id, r=Veykril
Allocate ast ids for parameters Since these can have attributes attached to them, we'll need this sooner or later (sooner being me tinkering with the token map right now)
2 parents f93b6ac + 2b9dde1 commit b3f4574

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

crates/hir-def/src/data.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use crate::{
1515
attr::Attrs,
1616
db::DefDatabase,
1717
expander::{Expander, Mark},
18-
item_tree::{
19-
self, AssocItem, FnFlags, ItemTree, ItemTreeId, MacroCall, ModItem, Param, TreeId,
20-
},
18+
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, MacroCall, ModItem, TreeId},
2119
macro_call_as_call_id, macro_id_to_def_id,
2220
nameres::{
2321
attr_resolution::ResolvedAttr,
@@ -69,7 +67,7 @@ impl FunctionData {
6967
let is_varargs = enabled_params
7068
.clone()
7169
.next_back()
72-
.map_or(false, |param| matches!(item_tree[param], Param::Varargs));
70+
.map_or(false, |param| item_tree[param].type_ref.is_none());
7371

7472
let mut flags = func.flags;
7573
if is_varargs {
@@ -105,10 +103,7 @@ impl FunctionData {
105103
name: func.name.clone(),
106104
params: enabled_params
107105
.clone()
108-
.filter_map(|id| match &item_tree[id] {
109-
Param::Normal(ty) => Some(ty.clone()),
110-
Param::Varargs => None,
111-
})
106+
.filter_map(|id| item_tree[id].type_ref.clone())
112107
.collect(),
113108
ret_type: func.ret_type.clone(),
114109
attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()),

crates/hir-def/src/item_tree.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,17 @@ pub struct Function {
613613
pub(crate) flags: FnFlags,
614614
}
615615

616-
#[derive(Debug, Clone, Eq, PartialEq)]
617-
pub enum Param {
618-
Normal(Interned<TypeRef>),
619-
Varargs,
616+
#[derive(Debug, Clone, PartialEq, Eq)]
617+
pub struct Param {
618+
/// This is [`None`] for varargs
619+
pub type_ref: Option<Interned<TypeRef>>,
620+
pub ast_id: ParamAstId,
621+
}
622+
623+
#[derive(Debug, Clone, PartialEq, Eq)]
624+
pub enum ParamAstId {
625+
Param(FileAstId<ast::Param>),
626+
SelfParam(FileAstId<ast::SelfParam>),
620627
}
621628

622629
bitflags::bitflags! {

crates/hir-def/src/item_tree/lower.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,12 @@ impl<'a> Ctx<'a> {
295295
}
296296
}
297297
};
298-
let ty = Interned::new(self_type);
299-
let idx = self.data().params.alloc(Param::Normal(ty));
298+
let type_ref = Interned::new(self_type);
299+
let ast_id = self.source_ast_id_map.ast_id(&self_param);
300+
let idx = self.data().params.alloc(Param {
301+
type_ref: Some(type_ref),
302+
ast_id: ParamAstId::SelfParam(ast_id),
303+
});
300304
self.add_attrs(
301305
idx.into(),
302306
RawAttrs::new(self.db.upcast(), &self_param, self.hygiene()),
@@ -305,11 +309,19 @@ impl<'a> Ctx<'a> {
305309
}
306310
for param in param_list.params() {
307311
let idx = match param.dotdotdot_token() {
308-
Some(_) => self.data().params.alloc(Param::Varargs),
312+
Some(_) => {
313+
let ast_id = self.source_ast_id_map.ast_id(&param);
314+
self.data()
315+
.params
316+
.alloc(Param { type_ref: None, ast_id: ParamAstId::Param(ast_id) })
317+
}
309318
None => {
310319
let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
311320
let ty = Interned::new(type_ref);
312-
self.data().params.alloc(Param::Normal(ty))
321+
let ast_id = self.source_ast_id_map.ast_id(&param);
322+
self.data()
323+
.params
324+
.alloc(Param { type_ref: Some(ty), ast_id: ParamAstId::Param(ast_id) })
313325
}
314326
};
315327
self.add_attrs(idx.into(), RawAttrs::new(self.db.upcast(), &param, self.hygiene()));

crates/hir-def/src/item_tree/pretty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,15 @@ impl Printer<'_> {
261261
self.indented(|this| {
262262
for param in params.clone() {
263263
this.print_attrs_of(param, "\n");
264-
match &this.tree[param] {
265-
Param::Normal(ty) => {
264+
match &this.tree[param].type_ref {
265+
Some(ty) => {
266266
if flags.contains(FnFlags::HAS_SELF_PARAM) {
267267
w!(this, "self: ");
268268
}
269269
this.print_type_ref(ty);
270270
wln!(this, ",");
271271
}
272-
Param::Varargs => {
272+
None => {
273273
wln!(this, "...");
274274
}
275275
};

crates/hir-expand/src/ast_id_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ register_ast_id_node! {
9999
TraitAlias,
100100
TypeAlias,
101101
Use,
102-
AssocItem, BlockExpr, Variant, RecordField, TupleField, ConstArg
102+
AssocItem, BlockExpr, Variant, RecordField, TupleField, ConstArg, Param, SelfParam
103103
}
104104

105105
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.

0 commit comments

Comments
 (0)