Skip to content

Commit

Permalink
Qualify compile_error!
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Mar 30, 2023
1 parent c58aceb commit 7109084
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,19 @@ impl ErrorMessage {
None => (Span::call_site(), Span::call_site()),
};

// compile_error!($message)
// std::compile_error!($message)
TokenStream::from_iter(vec![
TokenTree::Ident(Ident::new("std", start)),
TokenTree::Punct({
let mut punct = Punct::new(':', Spacing::Joint);
punct.set_span(start);
punct
}),
TokenTree::Punct({
let mut punct = Punct::new(':', Spacing::Alone);
punct.set_span(start);
punct
}),
TokenTree::Ident(Ident::new("compile_error", start)),
TokenTree::Punct({
let mut punct = Punct::new('!', Spacing::Alone);
Expand Down
18 changes: 18 additions & 0 deletions tests/test_compile_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use quote::quote;
use syn::Item;

#[test]
fn parse_crate_root_custom_inner_attribute() {
let tokens = quote! {
#![feature(custom_inner_attributes)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
};
let error = syn::parse2::<Item>(tokens).unwrap_err();
assert_eq!(
error.to_compile_error().to_string(),
r#"std :: compile_error ! { "expected square brackets" }"#
);
}

0 comments on commit 7109084

Please sign in to comment.