Skip to content

fix: Honor cfg attributes on params when lowering their patterns #13380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions crates/hir-def/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,20 @@ impl Body {
DefWithBodyId::FunctionId(f) => {
let f = f.lookup(db);
let src = f.source(db);
params = src.value.param_list();
params = src.value.param_list().map(|param_list| {
let item_tree = f.id.item_tree(db);
let func = &item_tree[f.id.value];
let krate = f.container.module(db).krate;
let crate_graph = db.crate_graph();
(
param_list,
func.params.clone().map(move |param| {
item_tree
.attrs(db, krate, param.into())
.is_cfg_enabled(&crate_graph[krate].cfg_options)
}),
)
});
(src.file_id, f.module(db), src.value.body().map(ast::Expr::from))
}
DefWithBodyId::ConstId(c) => {
Expand All @@ -334,6 +347,7 @@ impl Body {
let expander = Expander::new(db, file_id, module);
let (mut body, source_map) = Body::new(db, expander, params, body);
body.shrink_to_fit();

(Arc::new(body), Arc::new(source_map))
}

Expand Down Expand Up @@ -370,7 +384,7 @@ impl Body {
fn new(
db: &dyn DefDatabase,
expander: Expander,
params: Option<ast::ParamList>,
params: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
body: Option<ast::Expr>,
) -> (Body, BodySourceMap) {
lower::lower(db, expander, params, body)
Expand Down
16 changes: 11 additions & 5 deletions crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'a> LowerCtx<'a> {
pub(super) fn lower(
db: &dyn DefDatabase,
expander: Expander,
params: Option<ast::ParamList>,
params: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
body: Option<ast::Expr>,
) -> (Body, BodySourceMap) {
ExprCollector {
Expand Down Expand Up @@ -119,11 +119,13 @@ struct ExprCollector<'a> {
impl ExprCollector<'_> {
fn collect(
mut self,
param_list: Option<ast::ParamList>,
param_list: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
body: Option<ast::Expr>,
) -> (Body, BodySourceMap) {
if let Some(param_list) = param_list {
if let Some(self_param) = param_list.self_param() {
if let Some((param_list, mut attr_enabled)) = param_list {
if let Some(self_param) =
param_list.self_param().filter(|_| attr_enabled.next().unwrap_or(false))
{
let ptr = AstPtr::new(&self_param);
let param_pat = self.alloc_pat(
Pat::Bind {
Expand All @@ -139,7 +141,11 @@ impl ExprCollector<'_> {
self.body.params.push(param_pat);
}

for pat in param_list.params().filter_map(|param| param.pat()) {
for pat in param_list
.params()
.zip(attr_enabled)
.filter_map(|(param, enabled)| param.pat().filter(|_| enabled))
{
let param_pat = self.collect_pat(pat);
self.body.params.push(param_pat);
}
Expand Down
10 changes: 10 additions & 0 deletions crates/hir-ty/src/tests/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,3 +1070,13 @@ fn main() {
"#,
);
}

#[test]
fn cfg_params() {
check_types(
r#"
fn my_fn(#[cfg(feature = "feature")] u8: u8, u32: u32) {}
//^^^ u32
"#,
);
}
2 changes: 1 addition & 1 deletion crates/ide-completion/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! See `CompletionContext` structure.
//! See [`CompletionContext`] structure.

mod analysis;
#[cfg(test)]
Expand Down