Repository navigation
Tweak "name not found" resolution error when it happens from within a derive expansion #163380
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
Changes from all commits
28583f5
c77999a
2ac3993
746e155
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 |
|---|---|---|
|
|
@@ -32,3 +32,53 @@ pub fn derive(item: TokenStream) -> TokenStream { | |
| }; | ||
| tokens | ||
| } | ||
|
|
||
| #[proc_macro_derive(B)] | ||
| pub fn derive_2(item: TokenStream) -> TokenStream { | ||
| let mut tokens = item.into_iter(); | ||
| let _enum = tokens.next(); | ||
| let name = tokens.next().unwrap(); | ||
| let _ = tokens.next().unwrap(); | ||
| let _ = tokens.next().unwrap(); | ||
| let _ = tokens.next().unwrap(); | ||
| let TokenTree::Group(group) = tokens.next().unwrap() else { panic!() }; | ||
| let mut group = group.stream().into_iter(); | ||
| let variant = group.next().unwrap(); | ||
| let TokenTree::Group(args) = group.next().unwrap() else { panic!() }; | ||
| let arg = args.stream().into_iter().next().unwrap(); | ||
| let tokens = quote! { | ||
| trait X {} | ||
| impl X for $name {} | ||
|
|
||
| impl $name { | ||
| fn foo(&self) { | ||
| if let Self :: $variant(val) = self { | ||
| let _: T = val; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| }; | ||
| tokens | ||
| } | ||
|
|
||
| #[proc_macro_derive(C)] | ||
| pub fn derive_3(item: TokenStream) -> TokenStream { | ||
| let mut tokens = item.into_iter(); | ||
| let _enum = tokens.next(); | ||
| let name = tokens.next().unwrap(); | ||
| let _ = tokens.next().unwrap(); | ||
| let _ = tokens.next().unwrap(); | ||
| let _ = tokens.next().unwrap(); | ||
| let TokenTree::Group(group) = tokens.next().unwrap() else { panic!() }; | ||
| let mut group = group.stream().into_iter(); | ||
| let variant = group.next().unwrap(); | ||
| let TokenTree::Group(args) = group.next().unwrap() else { panic!() }; | ||
| let arg = args.stream().into_iter().next().unwrap(); | ||
| let tokens = quote! { | ||
| fn foo(val: $name<$arg>) { | ||
| $name :: $variant($arg) = val; | ||
| } | ||
| }; | ||
| tokens | ||
| } | ||
|
Contributor
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. ...but this macro is written such that if the name resolution error isn't emitted... |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,18 @@ enum A<T> { | |
| Variant(T), | ||
| //~^ ERROR: cannot find type `T` in this scope | ||
| } | ||
|
|
||
| // Resolution error comes entirely from within the derive macro: | ||
| #[derive(B)] //~ ERROR: cannot find type `T` in this scope | ||
| enum B<T> { | ||
| //~^ ERROR: missing generics for enum `B` | ||
| //~| ERROR: missing generics for enum `B` | ||
| Variant(T), | ||
| } | ||
|
|
||
| #[derive(C)] | ||
| enum C<T> { | ||
| Variant(T), | ||
| //~^ ERROR: cannot find value `T` in this scope | ||
| //~| ERROR: cannot find type `T` in this scope | ||
| } | ||
|
Comment on lines
+23
to
+28
Contributor
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. ...and this is the only code in the crate being built, we end up with an ICE. This is why I'm adding context instead of silencing. I'd love it if we could come up with a strategy where we silence in every other case, but not this one. |
||
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.
I'd originally made this delay the error as bug, so that the resolve error would not be present, under the assumption that other errors would already have been emitted (if it was a non-existing item, we'd have an error when evaluating the item itself, if a proc-macro error, we have the other errors)...
View changes since the review