From 710908453baa925203446db321c1423fd79d4eb2 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Thu, 30 Mar 2023 14:12:02 +0000 Subject: [PATCH 1/2] Qualify `compile_error!` --- src/error.rs | 13 ++++++++++++- tests/test_compile_error.rs | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/test_compile_error.rs diff --git a/src/error.rs b/src/error.rs index 7eebd243c4..b3a9e334ec 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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); diff --git a/tests/test_compile_error.rs b/tests/test_compile_error.rs new file mode 100644 index 0000000000..0b89f1adff --- /dev/null +++ b/tests/test_compile_error.rs @@ -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::(tokens).unwrap_err(); + assert_eq!( + error.to_compile_error().to_string(), + r#"std :: compile_error ! { "expected square brackets" }"# + ); +} From 07601a83cc3a5acf94b544d658f27d1ce0e20562 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Thu, 30 Mar 2023 14:36:23 +0000 Subject: [PATCH 2/2] Use `core` instead of `std` --- src/error.rs | 4 ++-- tests/test_compile_error.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index b3a9e334ec..2a0bcd9bd5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -278,9 +278,9 @@ impl ErrorMessage { None => (Span::call_site(), Span::call_site()), }; - // std::compile_error!($message) + // core::compile_error!($message) TokenStream::from_iter(vec![ - TokenTree::Ident(Ident::new("std", start)), + TokenTree::Ident(Ident::new("core", start)), TokenTree::Punct({ let mut punct = Punct::new(':', Spacing::Joint); punct.set_span(start); diff --git a/tests/test_compile_error.rs b/tests/test_compile_error.rs index 0b89f1adff..5433ccdd4c 100644 --- a/tests/test_compile_error.rs +++ b/tests/test_compile_error.rs @@ -13,6 +13,6 @@ fn parse_crate_root_custom_inner_attribute() { let error = syn::parse2::(tokens).unwrap_err(); assert_eq!( error.to_compile_error().to_string(), - r#"std :: compile_error ! { "expected square brackets" }"# + r#"core :: compile_error ! { "expected square brackets" }"# ); }