asm! parsing and lowering fixes

This commit is contained in:
Lukas Wirth 2024-09-05 15:06:37 +02:00
parent c075a9980e
commit f74a0c8801
10 changed files with 106 additions and 18 deletions

View file

@ -105,7 +105,7 @@ pub struct BodySourceMap {
// format_args!
FxHashMap<ExprId, Vec<(syntax::TextRange, Name)>>,
// asm!
FxHashMap<ExprId, Vec<(syntax::TextRange, usize)>>,
FxHashMap<ExprId, Vec<Vec<(syntax::TextRange, usize)>>>,
)>,
>,
@ -439,7 +439,7 @@ impl BodySourceMap {
pub fn asm_template_args(
&self,
node: InFile<&ast::AsmExpr>,
) -> Option<(ExprId, &[(syntax::TextRange, usize)])> {
) -> Option<(ExprId, &[Vec<(syntax::TextRange, usize)>])> {
let src = node.map(AstPtr::new).map(AstPtr::upcast::<ast::Expr>);
let expr = self.expr_map.get(&src)?;
Some(*expr).zip(self.template_map.as_ref()?.1.get(expr).map(std::ops::Deref::deref))
@ -487,7 +487,7 @@ impl BodySourceMap {
&self,
) -> Option<&(
FxHashMap<Idx<Expr>, Vec<(tt::TextRange, Name)>>,
FxHashMap<Idx<Expr>, Vec<(tt::TextRange, usize)>>,
FxHashMap<Idx<Expr>, Vec<Vec<(tt::TextRange, usize)>>>,
)> {
self.template_map.as_deref()
}

View file

@ -158,11 +158,14 @@ impl ExprCollector<'_> {
if !options.contains(AsmOptions::RAW) {
// Don't treat raw asm as a format string.
asm.template()
.filter_map(|it| Some((it.clone(), self.expand_macros_to_string(it)?)))
.for_each(|(expr, (s, is_direct_literal))| {
.enumerate()
.filter_map(|(idx, it)| Some((idx, it.clone(), self.expand_macros_to_string(it)?)))
.for_each(|(idx, expr, (s, is_direct_literal))| {
mappings.resize_with(idx + 1, Vec::default);
let Ok(text) = s.value() else {
return;
};
let mappings = &mut mappings[idx];
let template_snippet = match expr {
ast::Expr::Literal(literal) => match literal.kind() {
ast::LiteralKind::String(s) => Some(s.text().to_owned()),