mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-10-30 19:49:36 +00:00 
			
		
		
		
	Correctly handle attr macros placed in cfg_attr in speculative expansion
This commit is contained in:
		
							parent
							
								
									cc50868148
								
							
						
					
					
						commit
						7b64b407e8
					
				
					 1 changed files with 26 additions and 20 deletions
				
			
		|  | @ -13,7 +13,7 @@ use crate::{ | ||||||
|     AstId, BuiltinAttrExpander, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerCallInfo, |     AstId, BuiltinAttrExpander, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerCallInfo, | ||||||
|     EagerExpander, EditionedFileId, ExpandError, ExpandResult, ExpandTo, HirFileId, MacroCallId, |     EagerExpander, EditionedFileId, ExpandError, ExpandResult, ExpandTo, HirFileId, MacroCallId, | ||||||
|     MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind, |     MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind, | ||||||
|     attrs::{AttrId, collect_attrs}, |     attrs::{AttrId, AttrInput, RawAttrs, collect_attrs}, | ||||||
|     builtin::pseudo_derive_attr_expansion, |     builtin::pseudo_derive_attr_expansion, | ||||||
|     cfg_process, |     cfg_process, | ||||||
|     declarative::DeclarativeMacroExpander, |     declarative::DeclarativeMacroExpander, | ||||||
|  | @ -241,19 +241,10 @@ pub fn expand_speculative( | ||||||
| 
 | 
 | ||||||
|     let attr_arg = match loc.kind { |     let attr_arg = match loc.kind { | ||||||
|         MacroCallKind::Attr { invoc_attr_index, .. } => { |         MacroCallKind::Attr { invoc_attr_index, .. } => { | ||||||
|             let attr = if loc.def.is_attribute_derive() { |             if loc.def.is_attribute_derive() { | ||||||
|                 // for pseudo-derive expansion we actually pass the attribute itself only
 |                 // for pseudo-derive expansion we actually pass the attribute itself only
 | ||||||
|                 ast::Attr::cast(speculative_args.clone()) |                 ast::Attr::cast(speculative_args.clone()).and_then(|attr| attr.token_tree()).map( | ||||||
|             } else { |                     |token_tree| { | ||||||
|                 // Attributes may have an input token tree, build the subtree and map for this as well
 |  | ||||||
|                 // then try finding a token id for our token if it is inside this input subtree.
 |  | ||||||
|                 let item = ast::Item::cast(speculative_args.clone())?; |  | ||||||
|                 collect_attrs(&item) |  | ||||||
|                     .nth(invoc_attr_index.ast_index()) |  | ||||||
|                     .and_then(|x| Either::left(x.1)) |  | ||||||
|             }?; |  | ||||||
|             match attr.token_tree() { |  | ||||||
|                 Some(token_tree) => { |  | ||||||
|                         let mut tree = syntax_node_to_token_tree( |                         let mut tree = syntax_node_to_token_tree( | ||||||
|                             token_tree.syntax(), |                             token_tree.syntax(), | ||||||
|                             span_map, |                             span_map, | ||||||
|  | @ -261,10 +252,25 @@ pub fn expand_speculative( | ||||||
|                             DocCommentDesugarMode::ProcMacro, |                             DocCommentDesugarMode::ProcMacro, | ||||||
|                         ); |                         ); | ||||||
|                         *tree.top_subtree_delimiter_mut() = tt::Delimiter::invisible_spanned(span); |                         *tree.top_subtree_delimiter_mut() = tt::Delimiter::invisible_spanned(span); | ||||||
| 
 |                         tree | ||||||
|                     Some(tree) |                     }, | ||||||
|  |                 ) | ||||||
|  |             } else { | ||||||
|  |                 // Attributes may have an input token tree, build the subtree and map for this as well
 | ||||||
|  |                 // then try finding a token id for our token if it is inside this input subtree.
 | ||||||
|  |                 let item = ast::Item::cast(speculative_args.clone())?; | ||||||
|  |                 let attrs = RawAttrs::new_expanded(db, &item, span_map, loc.krate.cfg_options(db)); | ||||||
|  |                 attrs.iter().find(|attr| attr.id == invoc_attr_index).and_then(|attr| { | ||||||
|  |                     match attr.input.as_deref()? { | ||||||
|  |                         AttrInput::TokenTree(tt) => { | ||||||
|  |                             let mut attr_arg = tt.clone(); | ||||||
|  |                             attr_arg.top_subtree_delimiter_mut().kind = | ||||||
|  |                                 tt::DelimiterKind::Invisible; | ||||||
|  |                             Some(attr_arg) | ||||||
|                         } |                         } | ||||||
|                 _ => None, |                         AttrInput::Literal(_) => None, | ||||||
|  |                     } | ||||||
|  |                 }) | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         _ => None, |         _ => None, | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Chayim Refael Friedman
						Chayim Refael Friedman