-
Notifications
You must be signed in to change notification settings - Fork 14k
add transparent attribute for mod items #147709
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| use super::prelude::*; | ||
|
|
||
| pub(crate) struct TransparentParser; | ||
| impl<S: Stage> NoArgsAttributeParser<S> for TransparentParser { | ||
| const PATH: &[Symbol] = &[sym::transparent]; | ||
| const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; | ||
| const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Mod)]); | ||
| const CREATE: fn(Span) -> AttributeKind = AttributeKind::Transparent; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -658,6 +658,8 @@ declare_features! ( | |
| (unstable, trait_alias, "1.24.0", Some(41517)), | ||
| /// Allows for transmuting between arrays with sizes that contain generic consts. | ||
| (unstable, transmute_generic_consts, "1.70.0", Some(109929)), | ||
| /// Allows #[transparent] on modules to inherit lexical scopes. | ||
| (unstable, transparent_modules, "1.91.0", Some(79260)), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a real tracking issue |
||
| /// Allows #[repr(transparent)] on unions (RFC 2645). | ||
| (unstable, transparent_unions, "1.37.0", Some(60405)), | ||
| /// Allows inconsistent bounds in where clauses. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -524,17 +524,19 @@ enum ModuleKind { | |
| /// * A normal module – either `mod from_file;` or `mod from_block { }` – | ||
| /// or the crate root (which is conceptually a top-level module). | ||
| /// The crate root will have `None` for the symbol. | ||
| /// * the bool in the tuple next to symbol represents whether the "transparent" | ||
| /// attribute is present on the module, only applies to named `mod` items. | ||
| /// * A trait or an enum (it implicitly contains associated types, methods and variant | ||
| /// constructors). | ||
| Def(DefKind, DefId, Option<Symbol>), | ||
| Def(DefKind, DefId, Option<(Symbol, bool)>), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the most obvious place to put this but it doesn't feel great, don't love all the places where I changed shit to |
||
| } | ||
|
|
||
| impl ModuleKind { | ||
| /// Get name of the module. | ||
| fn name(&self) -> Option<Symbol> { | ||
| match *self { | ||
| ModuleKind::Block => None, | ||
| ModuleKind::Def(.., name) => name, | ||
| ModuleKind::Def(.., name_and_transparent) => name_and_transparent.map(|(name, _)| name), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -759,6 +761,13 @@ impl<'ra> Module<'ra> { | |
| } | ||
| } | ||
|
|
||
| fn is_transparent(self) -> bool { | ||
| match self.kind { | ||
| ModuleKind::Def(DefKind::Mod, _, Some((_, transparent))) => transparent, | ||
| _ => false, | ||
| } | ||
| } | ||
|
|
||
| fn is_ancestor_of(self, mut other: Self) -> bool { | ||
| while self != other { | ||
| if let Some(parent) = other.parent { | ||
|
|
@@ -2449,7 +2458,7 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> { | |
| if let ModuleKind::Def(.., name) = module.kind { | ||
| if let Some(parent) = module.parent { | ||
| // `unwrap` is safe: the presence of a parent means it's not the crate root. | ||
| names.push(name.unwrap()); | ||
| names.push(name.unwrap().0); | ||
| module = parent | ||
| } else { | ||
| break; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2230,6 +2230,7 @@ symbols! { | |
| transmute_unchecked, | ||
| transparent, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like I'm probably doing this part subtly wrong. The transparent symbol already exists and I'm using it, I tried seeing if I could stay away from it and find a way to have all the compiler internal stuff talk about it with a unique symbol ( |
||
| transparent_enums, | ||
| transparent_modules, | ||
| transparent_unions, | ||
| trivial_bounds, | ||
| truncf16, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //@ check-fail | ||
| //@ edition:2018 | ||
| // gate-test-transparent_modules | ||
| mod y { | ||
| macro_rules! s { | ||
| () => {}; | ||
| } | ||
|
|
||
| pub(crate) use s; | ||
| } | ||
|
|
||
| trait IWantAMethod { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you'll also want a test in which you use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: this does work correctly, I just tested it, but you still want to have a test for it |
||
| fn method(&self) {} | ||
| } | ||
|
|
||
| impl IWantAMethod for () {} | ||
|
|
||
| fn foo() { | ||
| struct S; | ||
| impl S { | ||
| const NAME: &'static str = "S"; | ||
| } | ||
| enum C { | ||
| B, | ||
| } | ||
|
|
||
| use y::s; | ||
| mod x { | ||
| // early resolution | ||
| s!(); //~ ERROR cannot find macro `s` in this scope | ||
| // late resolution | ||
| struct Y(S); //~ ERROR cannot find type `S` in this scope | ||
| impl Y { | ||
| // hir_typeck type dependent name resolution of associated const | ||
| const SNAME: &'static str = S::NAME; //~ ERROR failed to resolve: use of undeclared type `S` | ||
| } | ||
| fn bar() -> C { | ||
| //~^ ERROR cannot find type `C` in this scope | ||
| // method lookup, resolving appropriate trait in scope | ||
| ().method(); //~ ERROR no method named `method` found for unit type `()` in the current scope | ||
| // hir ty lowering type dependent name resolution of associated enum variant | ||
| C::B //~ ERROR failed to resolve: use of undeclared type `C` | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn main() {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| error: cannot find macro `s` in this scope | ||
| --> $DIR/no-transparent-fail.rs:30:9 | ||
| | | ||
| LL | s!(); | ||
| | ^ | ||
| | | ||
| = help: have you added the `#[macro_use]` on the module/import? | ||
| help: consider importing this macro through its public re-export | ||
| | | ||
| LL + use crate::y::s; | ||
| | | ||
|
|
||
| error[E0412]: cannot find type `S` in this scope | ||
| --> $DIR/no-transparent-fail.rs:32:18 | ||
| | | ||
| LL | struct Y(S); | ||
| | ^ not found in this scope | ||
| | | ||
| help: you might be missing a type parameter | ||
| | | ||
| LL | struct Y<S>(S); | ||
| | +++ | ||
|
|
||
| error[E0412]: cannot find type `C` in this scope | ||
| --> $DIR/no-transparent-fail.rs:37:21 | ||
| | | ||
| LL | struct Y(S); | ||
| | ------------ similarly named struct `Y` defined here | ||
| ... | ||
| LL | fn bar() -> C { | ||
| | ^ | ||
| | | ||
| help: a struct with a similar name exists | ||
| | | ||
| LL - fn bar() -> C { | ||
| LL + fn bar() -> Y { | ||
| | | ||
| help: you might be missing a type parameter | ||
| | | ||
| LL | fn bar<C>() -> C { | ||
| | +++ | ||
|
|
||
| error[E0433]: failed to resolve: use of undeclared type `S` | ||
| --> $DIR/no-transparent-fail.rs:35:41 | ||
| | | ||
| LL | const SNAME: &'static str = S::NAME; | ||
| | ^ | ||
| | | | ||
| | use of undeclared type `S` | ||
| | help: a struct with a similar name exists: `Y` | ||
|
|
||
| error[E0599]: no method named `method` found for unit type `()` in the current scope | ||
| --> $DIR/no-transparent-fail.rs:40:16 | ||
| | | ||
| LL | fn method(&self) {} | ||
| | ------ the method is available for `()` here | ||
| ... | ||
| LL | ().method(); | ||
| | ^^^^^^ method not found in `()` | ||
| | | ||
| = help: items from traits can only be used if the trait is in scope | ||
| help: trait `IWantAMethod` which provides `method` is implemented but not in scope; perhaps you want to import it | ||
| | | ||
| LL + use crate::IWantAMethod; | ||
| | | ||
|
|
||
| error[E0433]: failed to resolve: use of undeclared type `C` | ||
| --> $DIR/no-transparent-fail.rs:42:13 | ||
| | | ||
| LL | C::B | ||
| | ^ | ||
| | | | ||
| | use of undeclared type `C` | ||
| | help: a struct with a similar name exists: `Y` | ||
|
|
||
| error: aborting due to 6 previous errors | ||
|
|
||
| Some errors have detailed explanations: E0412, E0433, E0599. | ||
| For more information about an error, try `rustc --explain E0412`. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably also want to update this to point to actual documentation (I don't intend to add that, if its a blocker then leave this open or close it until someone comes along who wants to push this over the finish line)