Fix attribute index assignment in cfg_attr resolution

This commit is contained in:
Lukas Wirth 2021-03-16 19:55:40 +01:00
parent acc6458390
commit 3daa302cd3

View File

@ -136,16 +136,15 @@ pub(crate) fn filter(self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
let new_attrs = self let new_attrs = self
.iter() .iter()
.flat_map(|attr| -> SmallVec<[_; 1]> { .flat_map(|attr| -> SmallVec<[_; 1]> {
let attr = attr.clone();
let is_cfg_attr = let is_cfg_attr =
attr.path.as_ident().map_or(false, |name| *name == hir_expand::name![cfg_attr]); attr.path.as_ident().map_or(false, |name| *name == hir_expand::name![cfg_attr]);
if !is_cfg_attr { if !is_cfg_attr {
return smallvec![attr]; return smallvec![attr.clone()];
} }
let subtree = match &attr.input { let subtree = match &attr.input {
Some(AttrInput::TokenTree(it)) => it, Some(AttrInput::TokenTree(it)) => it,
_ => return smallvec![attr], _ => return smallvec![attr.clone()],
}; };
// Input subtree is: `(cfg, $(attr),+)` // Input subtree is: `(cfg, $(attr),+)`
@ -157,11 +156,14 @@ pub(crate) fn filter(self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
let cfg = parts.next().unwrap(); let cfg = parts.next().unwrap();
let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() };
let cfg = CfgExpr::parse(&cfg); let cfg = CfgExpr::parse(&cfg);
let index = attr.index;
let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| {
let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; let tree = Subtree { delimiter: None, token_trees: attr.to_vec() };
let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?;
let hygiene = Hygiene::new_unhygienic(); // FIXME // FIXME hygiene
Attr::from_src(attr, &hygiene) let hygiene = Hygiene::new_unhygienic();
// FIXME same index is assigned to multiple attributes
Attr::from_src(attr, &hygiene).map(|attr| Attr { index, ..attr })
}); });
let cfg_options = &crate_graph[krate].cfg_options; let cfg_options = &crate_graph[krate].cfg_options;