Skip to content

Conversation

kjeremy
Copy link
Contributor

@kjeremy kjeremy commented Nov 22, 2019

Does not validate that the input is a string literal. I thought that I could match_ast! against the macro_args but that did not work. Even if it had I am not sure which error would be appropriate.


let macro_content = {
// FIXME: verify that this is an ast::Literatal with kind() == LiteralKind.String, else error.
let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
Copy link
Contributor

@edwin0cheng edwin0cheng Nov 22, 2019

Choose a reason for hiding this comment

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

You could get it by using tt , if it is valid , it should be a single subtree with one literal token-tree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. Once I have the literal is there an existing funtion to validate it's type or do I need to manually parse the ends and look for quotes?

Copy link
Contributor

@edwin0cheng edwin0cheng Nov 22, 2019

Choose a reason for hiding this comment

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

As I know there is no existing function to validate it, so you may need to check whether it contains double quote. And if I remember correctly, the quote! macro support TokenTree too.

@edwin0cheng
Copy link
Contributor

Even if it had I am not sure which error would be appropriate.

I think we could add a builtin error in mbe::ExpandError::Builtin(String) for now.

@kjeremy kjeremy marked this pull request as ready for review November 22, 2019 20:16
@bjorn3
Copy link
Member

bjorn3 commented Nov 22, 2019

Could you actually give an error when compile_error is expanded?

@kjeremy
Copy link
Contributor Author

kjeremy commented Nov 22, 2019 via email

match &tt.token_trees[0] {
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => {
let s = it.text.as_str();
if s.starts_with(r#"""#) && s.ends_with(r#"""#) {
Copy link
Contributor

@edwin0cheng edwin0cheng Nov 23, 2019

Choose a reason for hiding this comment

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

It may be a raw string, which start with r# , just checking whether it contains a double quote will be enough.

@matklad
Copy link
Contributor

matklad commented Nov 24, 2019

Could you actually give an error when compile_error is expanded?

Nope, there's no infra for macro diagnostics yet

@kjeremy kjeremy force-pushed the expand-compile-error branch from 1f1963c to 67d3600 Compare November 25, 2019 00:02
@edwin0cheng
Copy link
Contributor

bors r+ , Thanks

bors bot added a commit that referenced this pull request Nov 25, 2019
2362: Expand compile_error! r=edwin0cheng a=kjeremy

Does not validate that the input is a string literal. I thought that I could `match_ast!` against the `macro_args` but that did not work. Even if it had I am not sure which error would be appropriate.

Co-authored-by: Jeremy Kolb <kjeremy@gmail.com>
@bors
Copy link
Contributor

bors bot commented Nov 25, 2019

Build succeeded

  • Rust
  • TypeScript

@bors bors bot merged commit 67d3600 into rust-lang:master Nov 25, 2019
@kjeremy kjeremy deleted the expand-compile-error branch November 25, 2019 14:48
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.

4 participants