Skip to content

Commit deddad3

Browse files
committed
Auto merge of rust-lang#13380 - Veykril:cfg-pat-params, r=Veykril
Honor cfg attributes on params when lowering their patterns Closes rust-lang/rust-analyzer#13375
2 parents ef014aa + 63ed71b commit deddad3

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

crates/hir-def/src/body.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,20 @@ impl Body {
311311
DefWithBodyId::FunctionId(f) => {
312312
let f = f.lookup(db);
313313
let src = f.source(db);
314-
params = src.value.param_list();
314+
params = src.value.param_list().map(|param_list| {
315+
let item_tree = f.id.item_tree(db);
316+
let func = &item_tree[f.id.value];
317+
let krate = f.container.module(db).krate;
318+
let crate_graph = db.crate_graph();
319+
(
320+
param_list,
321+
func.params.clone().map(move |param| {
322+
item_tree
323+
.attrs(db, krate, param.into())
324+
.is_cfg_enabled(&crate_graph[krate].cfg_options)
325+
}),
326+
)
327+
});
315328
(src.file_id, f.module(db), src.value.body().map(ast::Expr::from))
316329
}
317330
DefWithBodyId::ConstId(c) => {
@@ -334,6 +347,7 @@ impl Body {
334347
let expander = Expander::new(db, file_id, module);
335348
let (mut body, source_map) = Body::new(db, expander, params, body);
336349
body.shrink_to_fit();
350+
337351
(Arc::new(body), Arc::new(source_map))
338352
}
339353

@@ -370,7 +384,7 @@ impl Body {
370384
fn new(
371385
db: &dyn DefDatabase,
372386
expander: Expander,
373-
params: Option<ast::ParamList>,
387+
params: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
374388
body: Option<ast::Expr>,
375389
) -> (Body, BodySourceMap) {
376390
lower::lower(db, expander, params, body)

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a> LowerCtx<'a> {
7777
pub(super) fn lower(
7878
db: &dyn DefDatabase,
7979
expander: Expander,
80-
params: Option<ast::ParamList>,
80+
params: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
8181
body: Option<ast::Expr>,
8282
) -> (Body, BodySourceMap) {
8383
ExprCollector {
@@ -119,11 +119,13 @@ struct ExprCollector<'a> {
119119
impl ExprCollector<'_> {
120120
fn collect(
121121
mut self,
122-
param_list: Option<ast::ParamList>,
122+
param_list: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
123123
body: Option<ast::Expr>,
124124
) -> (Body, BodySourceMap) {
125-
if let Some(param_list) = param_list {
126-
if let Some(self_param) = param_list.self_param() {
125+
if let Some((param_list, mut attr_enabled)) = param_list {
126+
if let Some(self_param) =
127+
param_list.self_param().filter(|_| attr_enabled.next().unwrap_or(false))
128+
{
127129
let ptr = AstPtr::new(&self_param);
128130
let param_pat = self.alloc_pat(
129131
Pat::Bind {
@@ -139,7 +141,11 @@ impl ExprCollector<'_> {
139141
self.body.params.push(param_pat);
140142
}
141143

142-
for pat in param_list.params().filter_map(|param| param.pat()) {
144+
for pat in param_list
145+
.params()
146+
.zip(attr_enabled)
147+
.filter_map(|(param, enabled)| param.pat().filter(|_| enabled))
148+
{
143149
let param_pat = self.collect_pat(pat);
144150
self.body.params.push(param_pat);
145151
}

crates/hir-ty/src/tests/patterns.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,3 +1070,13 @@ fn main() {
10701070
"#,
10711071
);
10721072
}
1073+
1074+
#[test]
1075+
fn cfg_params() {
1076+
check_types(
1077+
r#"
1078+
fn my_fn(#[cfg(feature = "feature")] u8: u8, u32: u32) {}
1079+
//^^^ u32
1080+
"#,
1081+
);
1082+
}

crates/ide-completion/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! See `CompletionContext` structure.
1+
//! See [`CompletionContext`] structure.
22
33
mod analysis;
44
#[cfg(test)]

0 commit comments

Comments
 (0)