From ba0e4c745d60ea1a0e32d1469648394410f6c450 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 17 Mar 2021 18:28:00 +0100 Subject: Apply `#[cfg]`s when computing function signatures --- crates/hir_def/src/data.rs | 15 +++++++++++---- crates/hir_def/src/item_tree.rs | 6 ++++++ 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index f9bb0c6da..e976e419e 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs @@ -36,18 +36,25 @@ impl FunctionData { pub(crate) fn fn_data_query(db: &dyn DefDatabase, func: FunctionId) -> Arc { let loc = func.lookup(db); let krate = loc.container.module(db).krate; + let crate_graph = db.crate_graph(); + let cfg_options = &crate_graph[krate].cfg_options; let item_tree = db.item_tree(loc.id.file_id); let func = &item_tree[loc.id.value]; - let is_varargs = func + + let enabled_params = func .params .clone() - .last() + .filter(|¶m| item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options)); + + // If last cfg-enabled param is a `...` param, it's a varargs function. + let is_varargs = enabled_params + .clone() + .next_back() .map_or(false, |param| matches!(item_tree[param], Param::Varargs)); Arc::new(FunctionData { name: func.name.clone(), - params: func - .params + params: enabled_params .clone() .filter_map(|id| match &item_tree[id] { Param::Normal(ty) => Some(item_tree[*ty].clone()), diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index c14da95ab..90df3d929 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -824,6 +824,12 @@ impl Iterator for IdRange { } } +impl DoubleEndedIterator for IdRange { + fn next_back(&mut self) -> Option { + self.range.next_back().map(|raw| Idx::from_raw(raw.into())) + } +} + impl fmt::Debug for IdRange { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple(&format!("IdRange::<{}>", type_name::())).field(&self.range).finish() -- cgit v1.2.3