-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement concat eager macro #3392
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
Conversation
e1e69c4
to
84effda
Compare
@matklad It is ready to review now. |
Haven't looked closely yet, but overall it looks good to me! |
@@ -208,23 +245,44 @@ fn format_args_expand( | |||
Ok(expanded) | |||
} | |||
|
|||
fn unquote_str(lit: &tt::Literal) -> Option<String> { | |||
let lit = ast::make::tokens::literal(&lit.to_string()); | |||
let token = ast::String::cast(lit)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long term, we probably should have a way to uquote a tt
without going via AST, but this totally fine for the time being
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I implement this functions, I tried to find whether we have a util functions for unquoting string and I found this and other one inside ra_syntax
(validate_literal
). I would be happy to implement one but I don't know where should it in, Maybe ra_fmt
? Anyway it will be another PR.
crates/ra_hir_expand/src/eager.rs
Outdated
|
||
pub fn expand_eager_macro( | ||
db: &impl AstDatabase, | ||
macro_call: InFile<ast::MacroCall>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, now that i think about it, I guess the right primitive is eager expansion of macro argument. In format!("{}", 92)
the first argument, "{}"
is expanded eagerly (so you can do format!(concat!(file!(), ": {}"), 92)
), but the rest are expanded normally.
Not sure what's best: merge the current approach with EagerMacroId
and then try to migrate it to EagerMacroArgumentId
, or to do the refactor before merging the PR.
I lean towards merging now and refactoring later though :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this one ?
format!(concat!("{","}"), concat!("Hell","o"));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second concat!
here, itself, is expanded lazily
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Um.. I think format!
is not a good example here since it is not a eager macro itself. format_args
(which format! use) would be a better one. This macro produces a value of type fmt::Arguments
and if i understand correctly, to produce that value, all arguments inside format_args
have to be expanded beforehand. Or I still miss something ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think format! is not a good example here since it is not a eager macro itself.
That's precisely my point: it's not the macro that is eager, it's the argument. For format, I believe the first arg is expanded eagerly, and the rest lazily. But I might be missing some details. It might make sense to consult rustc to check if first&rest args of format are treated differently .
Yeah, I moved the |
bors r+ |
3392: Implement concat eager macro r=matklad a=edwin0cheng This PR implements the following things: 1. Add basic eager macro infrastructure by introducing `EagerCallId` such that the new `MacroCallId` is defined as : ``` #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum MacroCallId { LazyMacro(LazyMacroId), EagerMacro(EagerMacroId), } ``` 2. Add `concat!` builtin macro. Co-authored-by: Edwin Cheng <[email protected]>
Build failed |
cf830ae
to
4d5e80c
Compare
bors retry |
Build succeeded |
I think this broke
|
This PR implements the following things:
EagerCallId
such that the newMacroCallId
is defined as :concat!
builtin macro.