Skip to content

Implement format_args #2489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 6, 2019
Merged

Implement format_args #2489

merged 7 commits into from
Dec 6, 2019

Conversation

flodiebold
Copy link
Member

This fixes a huge amount of type mismatches (because every format call was a type mismatch so far); I also hoped to get go to def working within format! etc., and the test says it should, but in practice it still doesn't seem to...

Also remove the len parameter from Name::new_inline_ascii, which I'm assuming was only there because of const fn limitations?

cc @edwin0cheng

@flodiebold flodiebold requested a review from matklad December 6, 2019 18:52
@edwin0cheng
Copy link
Member

Oh... nice, but If I remember correctly, format_args! should be eager expansion ?
e.g.

fn main() {
    format!(concat!("ABC","DEF"));
}

@flodiebold
Copy link
Member Author

flodiebold commented Dec 6, 2019

I don't think that really matters for now, I ignore the format string anyway 😅

(Edit: Yeah, it's just the format string being eagerly expanded...)

@edwin0cheng
Copy link
Member

edwin0cheng commented Dec 6, 2019

to get go to def working within format! and the test says it should, but in practice it still doesn't seem to...

I think it is because :

https://github.com/rust-analyzer/rust-analyzer/blob/a25e8cff6755c1a0c46ee3690dcd75b641c0b3e4/crates/ra_hir_expand/src/db.rs#L45-L49

We should set it to mbe::Origin::Call , right now quote! macro only produce tt::TokenId::unspecific , so all builtin macro def do not produce any TokenId.

@flodiebold
Copy link
Member Author

Just setting it to mbe::Origin::Call doesn't seem to do it 😕

Copy link
Member

@matklad matklad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

let parent_ancestors: Box<dyn Iterator<Item = InFile<SyntaxNode>>> =
Box::new(parent_node.into_iter().flat_map(move |n| ancestors_with_macros(db, n.as_ref())));
node.value.ancestors().map(move |n| file.with_value(n)).chain(parent_ancestors)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this will be a hugely important function in the end, so, if you feel like it, you can implement a struct-based non-allocating iterator

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maaaybe even in the form of inherent method on InFile?

@flodiebold
Copy link
Member Author

The problem seems to be here, we're not getting an expr ID 😕

Just enough to fix the huge amount of type mismatches they cause.
I assume it was previously required because `len` was not const, but that
doesn't seem to be a problem anymore.
SourceAnalyzer didn't work properly within expression macro expansions because
it didn't find the enclosing function. Fix this by going up the expansion chain
to find ancestors. This makes the test work, but apparently in real usage it's
still not working.
self,
db: &'a impl crate::db::AstDatabase,
) -> impl Iterator<Item = InFile<SyntaxNode>> + 'a {
SyntaxNodeAncestorsWithMacros { db, node: Some(self) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, seems like std::iter::successors would also work here?

Copy link
Member Author

@flodiebold flodiebold Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, actually...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flodiebold
Copy link
Member Author

bors r+

bors bot added a commit that referenced this pull request Dec 6, 2019
2489: Implement `format_args` r=flodiebold a=flodiebold

This fixes a huge amount of type mismatches (because every format call was a type mismatch so far); I also hoped to get go to def working within `format!` etc., and the test says it should, but in practice it still doesn't seem to...

Also remove the `len` parameter from `Name::new_inline_ascii`, which I'm assuming was only there because of `const fn` limitations?

cc @edwin0cheng 

Co-authored-by: Florian Diebold <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 6, 2019

Build succeeded

  • Rust
  • TypeScript

@bors bors bot merged commit b2c01f4 into rust-lang:master Dec 6, 2019
@flodiebold flodiebold deleted the format_args branch December 6, 2019 21:19
@flodiebold flodiebold mentioned this pull request Dec 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants