-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Improve and test cross-crate hygiene #90202
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ecb49e
Handle cross-crate module `ExpnId`s consistently
matthewjasper 1536d72
Don't suggest importing items with hygienic names
matthewjasper fabede1
Add more tests for cross-crate hygiene
matthewjasper d8426ea
Remove `ModData` from rustc_metadata
matthewjasper a76a2d4
Add comments to hygiene tests
matthewjasper 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
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
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,73 @@ | ||
#![feature(decl_macro)] | ||
|
||
#[derive(Copy, Clone, PartialEq, Debug)] | ||
pub enum Field { | ||
RootCtxt, | ||
MacroCtxt, | ||
} | ||
|
||
#[rustfmt::skip] | ||
macro x( | ||
$macro_name:ident, | ||
$macro2_name:ident, | ||
$type_name:ident, | ||
$field_name:ident, | ||
$const_name:ident | ||
) { | ||
#[derive(Copy, Clone)] | ||
pub struct $type_name { | ||
pub field: Field, | ||
pub $field_name: Field, | ||
} | ||
|
||
pub const $const_name: $type_name = | ||
$type_name { field: Field::MacroCtxt, $field_name: Field::RootCtxt }; | ||
|
||
#[macro_export] | ||
macro_rules! $macro_name { | ||
(check_fields_of $e:expr) => {{ | ||
let e = $e; | ||
assert_eq!(e.field, Field::MacroCtxt); | ||
assert_eq!(e.$field_name, Field::RootCtxt); | ||
}}; | ||
(check_fields) => {{ | ||
assert_eq!($const_name.field, Field::MacroCtxt); | ||
assert_eq!($const_name.$field_name, Field::RootCtxt); | ||
}}; | ||
(construct) => { | ||
$type_name { field: Field::MacroCtxt, $field_name: Field::RootCtxt } | ||
}; | ||
} | ||
|
||
pub macro $macro2_name { | ||
(check_fields_of $e:expr) => {{ | ||
let e = $e; | ||
assert_eq!(e.field, Field::MacroCtxt); | ||
assert_eq!(e.$field_name, Field::RootCtxt); | ||
}}, | ||
(check_fields) => {{ | ||
assert_eq!($const_name.field, Field::MacroCtxt); | ||
assert_eq!($const_name.$field_name, Field::RootCtxt); | ||
}}, | ||
(construct) => { | ||
$type_name { field: Field::MacroCtxt, $field_name: Field::RootCtxt } | ||
} | ||
} | ||
} | ||
|
||
x!(test_fields, test_fields2, MyStruct, field, MY_CONST); | ||
|
||
pub fn check_fields(s: MyStruct) { | ||
test_fields!(check_fields_of s); | ||
} | ||
|
||
pub fn check_fields_local() { | ||
test_fields!(check_fields); | ||
test_fields2!(check_fields); | ||
|
||
let s1 = test_fields!(construct); | ||
test_fields!(check_fields_of s1); | ||
|
||
let s2 = test_fields2!(construct); | ||
test_fields2!(check_fields_of s2); | ||
} |
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,160 @@ | ||
#![feature(decl_macro)] | ||
|
||
#[derive(PartialEq, Eq, Debug)] | ||
pub enum Method { | ||
DefaultMacroCtxt, | ||
DefaultRootCtxt, | ||
OverrideMacroCtxt, | ||
OverrideRootCtxt, | ||
} | ||
|
||
#[rustfmt::skip] | ||
macro x($macro_name:ident, $macro2_name:ident, $trait_name:ident, $method_name:ident) { | ||
pub trait $trait_name { | ||
fn method(&self) -> Method { | ||
Method::DefaultMacroCtxt | ||
} | ||
|
||
fn $method_name(&self) -> Method { | ||
Method::DefaultRootCtxt | ||
} | ||
} | ||
|
||
impl $trait_name for () {} | ||
impl $trait_name for bool { | ||
fn method(&self) -> Method { | ||
Method::OverrideMacroCtxt | ||
} | ||
|
||
fn $method_name(&self) -> Method { | ||
Method::OverrideRootCtxt | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! $macro_name { | ||
(check_resolutions) => { | ||
assert_eq!(().method(), Method::DefaultMacroCtxt); | ||
assert_eq!($trait_name::method(&()), Method::DefaultMacroCtxt); | ||
assert_eq!(().$method_name(), Method::DefaultRootCtxt); | ||
assert_eq!($trait_name::$method_name(&()), Method::DefaultRootCtxt); | ||
|
||
assert_eq!(false.method(), Method::OverrideMacroCtxt); | ||
assert_eq!($trait_name::method(&false), Method::OverrideMacroCtxt); | ||
assert_eq!(false.$method_name(), Method::OverrideRootCtxt); | ||
assert_eq!($trait_name::$method_name(&false), Method::OverrideRootCtxt); | ||
|
||
assert_eq!('a'.method(), Method::DefaultMacroCtxt); | ||
assert_eq!($trait_name::method(&'a'), Method::DefaultMacroCtxt); | ||
assert_eq!('a'.$method_name(), Method::DefaultRootCtxt); | ||
assert_eq!($trait_name::$method_name(&'a'), Method::DefaultRootCtxt); | ||
|
||
assert_eq!(1i32.method(), Method::OverrideMacroCtxt); | ||
assert_eq!($trait_name::method(&1i32), Method::OverrideMacroCtxt); | ||
assert_eq!(1i32.$method_name(), Method::OverrideRootCtxt); | ||
assert_eq!($trait_name::$method_name(&1i32), Method::OverrideRootCtxt); | ||
|
||
assert_eq!(1i64.method(), Method::OverrideMacroCtxt); | ||
assert_eq!($trait_name::method(&1i64), Method::OverrideMacroCtxt); | ||
assert_eq!(1i64.$method_name(), Method::OverrideRootCtxt); | ||
assert_eq!($trait_name::$method_name(&1i64), Method::OverrideRootCtxt); | ||
}; | ||
(assert_no_override $v:expr) => { | ||
assert_eq!($v.method(), Method::DefaultMacroCtxt); | ||
assert_eq!($trait_name::method(&$v), Method::DefaultMacroCtxt); | ||
assert_eq!($v.$method_name(), Method::DefaultRootCtxt); | ||
assert_eq!($trait_name::$method_name(&$v), Method::DefaultRootCtxt); | ||
}; | ||
(assert_override $v:expr) => { | ||
assert_eq!($v.method(), Method::OverrideMacroCtxt); | ||
assert_eq!($trait_name::method(&$v), Method::OverrideMacroCtxt); | ||
assert_eq!($v.$method_name(), Method::OverrideRootCtxt); | ||
assert_eq!($trait_name::$method_name(&$v), Method::OverrideRootCtxt); | ||
}; | ||
(impl for $t:ty) => { | ||
impl $trait_name for $t { | ||
fn method(&self) -> Method { | ||
Method::OverrideMacroCtxt | ||
} | ||
|
||
fn $method_name(&self) -> Method { | ||
Method::OverrideRootCtxt | ||
} | ||
} | ||
}; | ||
} | ||
|
||
pub macro $macro2_name { | ||
(check_resolutions) => { | ||
assert_eq!(().method(), Method::DefaultMacroCtxt); | ||
assert_eq!($trait_name::method(&()), Method::DefaultMacroCtxt); | ||
assert_eq!(().$method_name(), Method::DefaultRootCtxt); | ||
assert_eq!($trait_name::$method_name(&()), Method::DefaultRootCtxt); | ||
|
||
assert_eq!(false.method(), Method::OverrideMacroCtxt); | ||
assert_eq!($trait_name::method(&false), Method::OverrideMacroCtxt); | ||
assert_eq!(false.$method_name(), Method::OverrideRootCtxt); | ||
assert_eq!($trait_name::$method_name(&false), Method::OverrideRootCtxt); | ||
|
||
assert_eq!('a'.method(), Method::DefaultMacroCtxt); | ||
assert_eq!($trait_name::method(&'a'), Method::DefaultMacroCtxt); | ||
assert_eq!('a'.$method_name(), Method::DefaultRootCtxt); | ||
assert_eq!($trait_name::$method_name(&'a'), Method::DefaultRootCtxt); | ||
|
||
assert_eq!(1i32.method(), Method::OverrideMacroCtxt); | ||
assert_eq!($trait_name::method(&1i32), Method::OverrideMacroCtxt); | ||
assert_eq!(1i32.$method_name(), Method::OverrideRootCtxt); | ||
assert_eq!($trait_name::$method_name(&1i32), Method::OverrideRootCtxt); | ||
|
||
assert_eq!(1i64.method(), Method::OverrideMacroCtxt); | ||
assert_eq!($trait_name::method(&1i64), Method::OverrideMacroCtxt); | ||
assert_eq!(1i64.$method_name(), Method::OverrideRootCtxt); | ||
assert_eq!($trait_name::$method_name(&1i64), Method::OverrideRootCtxt); | ||
}, | ||
(assert_no_override $v:expr) => { | ||
assert_eq!($v.method(), Method::DefaultMacroCtxt); | ||
assert_eq!($trait_name::method(&$v), Method::DefaultMacroCtxt); | ||
assert_eq!($v.$method_name(), Method::DefaultRootCtxt); | ||
assert_eq!($trait_name::$method_name(&$v), Method::DefaultRootCtxt); | ||
}, | ||
(assert_override $v:expr) => { | ||
assert_eq!($v.method(), Method::OverrideMacroCtxt); | ||
assert_eq!($trait_name::method(&$v), Method::OverrideMacroCtxt); | ||
assert_eq!($v.$method_name(), Method::OverrideRootCtxt); | ||
assert_eq!($trait_name::$method_name(&$v), Method::OverrideRootCtxt); | ||
}, | ||
(impl for $t:ty) => { | ||
impl $trait_name for $t { | ||
fn method(&self) -> Method { | ||
Method::OverrideMacroCtxt | ||
} | ||
|
||
fn $method_name(&self) -> Method { | ||
Method::OverrideRootCtxt | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
x!(test_trait, test_trait2, MyTrait, method); | ||
|
||
impl MyTrait for char {} | ||
test_trait!(impl for i32); | ||
test_trait2!(impl for i64); | ||
|
||
pub fn check_crate_local() { | ||
test_trait!(check_resolutions); | ||
test_trait2!(check_resolutions); | ||
} | ||
|
||
// Check that any comparison of idents at monomorphization time is correct | ||
pub fn check_crate_local_generic<T: MyTrait, U: MyTrait>(t: T, u: U) { | ||
test_trait!(check_resolutions); | ||
test_trait2!(check_resolutions); | ||
|
||
test_trait!(assert_no_override t); | ||
test_trait2!(assert_no_override t); | ||
test_trait!(assert_override u); | ||
test_trait2!(assert_override u); | ||
} |
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,7 @@ | ||
#![feature(decl_macro)] | ||
|
||
macro x() { | ||
pub struct MyStruct; | ||
} | ||
|
||
x!(); |
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,15 @@ | ||
#![feature(decl_macro)] | ||
|
||
macro x($macro_name:ident) { | ||
#[macro_export] | ||
macro_rules! $macro_name { | ||
(define) => { | ||
pub struct MyStruct; | ||
}; | ||
(create) => { | ||
MyStruct {} | ||
}; | ||
} | ||
} | ||
|
||
x!(my_struct); |
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,36 @@ | ||
#![feature(decl_macro)] | ||
|
||
#[rustfmt::skip] | ||
macro x($macro_name:ident, $macro2_name:ident, $type_name:ident, $variant_name:ident) { | ||
#[repr(u8)] | ||
pub enum $type_name { | ||
Variant = 0, | ||
$variant_name = 1, | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! $macro_name { | ||
() => {{ | ||
assert_eq!($type_name::Variant as u8, 0); | ||
assert_eq!($type_name::$variant_name as u8, 1); | ||
assert_eq!(<$type_name>::Variant as u8, 0); | ||
assert_eq!(<$type_name>::$variant_name as u8, 1); | ||
}}; | ||
} | ||
|
||
pub macro $macro2_name { | ||
() => {{ | ||
assert_eq!($type_name::Variant as u8, 0); | ||
assert_eq!($type_name::$variant_name as u8, 1); | ||
assert_eq!(<$type_name>::Variant as u8, 0); | ||
assert_eq!(<$type_name>::$variant_name as u8, 1); | ||
}}, | ||
} | ||
} | ||
|
||
x!(test_variants, test_variants2, MyEnum, Variant); | ||
|
||
pub fn check_variants() { | ||
test_variants!(); | ||
test_variants2!(); | ||
} |
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,14 @@ | ||
// check-pass | ||
// aux-build:use_by_macro.rs | ||
|
||
#![feature(type_name_of_val)] | ||
extern crate use_by_macro; | ||
|
||
use use_by_macro::*; | ||
|
||
enum MyStruct {} | ||
my_struct!(define); | ||
|
||
fn main() { | ||
let x = my_struct!(create); | ||
} |
Oops, something went wrong.
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.