Honor cfg attributes on params when lowering their patterns

This commit is contained in:
Lukas Wirth 2022-10-10 09:47:09 +02:00
parent 476d043874
commit 63ed71bd30
4 changed files with 38 additions and 8 deletions

View File

@ -311,7 +311,20 @@ pub(crate) fn body_with_source_map_query(
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) => {
@ -334,6 +347,7 @@ pub(crate) fn body_with_source_map_query(
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))
}
@ -370,7 +384,7 @@ pub fn pretty_print(&self, db: &dyn DefDatabase, owner: DefWithBodyId) -> String
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)

View File

@ -77,7 +77,7 @@ pub(crate) fn ast_id<N: AstNode>(&self, db: &dyn DefDatabase, item: &N) -> Optio
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 {
@ -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 {
@ -139,7 +141,11 @@ fn collect(
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);
}

View File

@ -1070,3 +1070,13 @@ enum Option<T> {Some(T), None}
"#,
);
}
#[test]
fn cfg_params() {
check_types(
r#"
fn my_fn(#[cfg(feature = "feature")] u8: u8, u32: u32) {}
//^^^ u32
"#,
);
}

View File

@ -1,4 +1,4 @@
//! See `CompletionContext` structure.
//! See [`CompletionContext`] structure.
mod analysis;
#[cfg(test)]