-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve error handling in symbols
proc-macro
#79944
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use super::*; | ||
|
||
// This test is mainly here for interactive development. Use this test while | ||
// you're working on the proc-macro defined in this file. | ||
#[test] | ||
fn test_symbols() { | ||
// We textually include the symbol.rs file, which contains the list of all | ||
// symbols, keywords, and common words. Then we search for the | ||
// `symbols! { ... }` call. | ||
|
||
static SYMBOL_RS_FILE: &str = include_str!("../../../rustc_span/src/symbol.rs"); | ||
|
||
let file = syn::parse_file(SYMBOL_RS_FILE).unwrap(); | ||
let symbols_path: syn::Path = syn::parse_quote!(symbols); | ||
|
||
let m: &syn::ItemMacro = file | ||
.items | ||
.iter() | ||
.filter_map(|i| { | ||
if let syn::Item::Macro(m) = i { | ||
if m.mac.path == symbols_path { Some(m) } else { None } | ||
} else { | ||
None | ||
} | ||
}) | ||
.next() | ||
.expect("did not find `symbols!` macro invocation."); | ||
|
||
let body_tokens = m.mac.tokens.clone(); | ||
|
||
test_symbols_macro(body_tokens, &[]); | ||
} | ||
|
||
fn test_symbols_macro(input: TokenStream, expected_errors: &[&str]) { | ||
let (output, found_errors) = symbols_with_errors(input); | ||
|
||
// It should always parse. | ||
let _parsed_file = syn::parse2::<syn::File>(output).unwrap(); | ||
|
||
assert_eq!( | ||
found_errors.len(), | ||
expected_errors.len(), | ||
"Macro generated a different number of errors than expected" | ||
); | ||
|
||
for (found_error, &expected_error) in found_errors.iter().zip(expected_errors.iter()) { | ||
let found_error_str = format!("{}", found_error); | ||
assert_eq!(found_error_str, expected_error); | ||
} | ||
} | ||
|
||
#[test] | ||
fn check_dup_keywords() { | ||
let input = quote! { | ||
Keywords { | ||
Crate: "crate", | ||
Crate: "crate", | ||
} | ||
Symbols {} | ||
}; | ||
test_symbols_macro(input, &["Symbol `crate` is duplicated", "location of previous definition"]); | ||
} | ||
|
||
#[test] | ||
fn check_dup_symbol() { | ||
let input = quote! { | ||
Keywords {} | ||
Symbols { | ||
splat, | ||
splat, | ||
} | ||
}; | ||
test_symbols_macro(input, &["Symbol `splat` is duplicated", "location of previous definition"]); | ||
} | ||
|
||
#[test] | ||
fn check_dup_symbol_and_keyword() { | ||
let input = quote! { | ||
Keywords { | ||
Splat: "splat", | ||
} | ||
Symbols { | ||
splat, | ||
} | ||
}; | ||
test_symbols_macro(input, &["Symbol `splat` is duplicated", "location of previous definition"]); | ||
} | ||
|
||
#[test] | ||
fn check_symbol_order() { | ||
let input = quote! { | ||
Keywords {} | ||
Symbols { | ||
zebra, | ||
aardvark, | ||
} | ||
}; | ||
test_symbols_macro( | ||
input, | ||
&["Symbol `aardvark` must precede `zebra`", "location of previous symbol `zebra`"], | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.