Improve Suggestion from the Compiler Specific Case?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.2k
- PR merge metrics
- PR metrics pending
Description
Code
lib.rs
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemStruct};
#[proc_macro_attribute]
pub fn foo_bar_attribute(_metadata: TokenStream, _input: TokenStream) -> TokenStream {
let input = parse_macro_input!(_input as ItemStruct);
let struct_name = &input.ident;`
TokenStream::from(quote! {
#[derive(Debug)]
struct #struct_name {
foo: i32,
bar: i32
}
impl Default for #struct_name {
fn default() -> Self {
#struct_name { foo: 10. bar: 20 } <-- syntax error here '.' instead of ','
}
}
impl #struct_name {
fn double_foo(&self) -> i32 {
self.foo * 2
}
}
})
}
_main.rs_
use macro_demo::*;
#[foo_bar_attribute] <<----- error point to here
struct MyStruct {
baz: i32,
}
fn main() {
let demo = MyStruct::default();
println!("struct is {:?}", demo);
let double_foo = demo.double_foo();
println!("double foo: {}", double_foo);
}
Current output
error: expected one of `,`, `.`, `?`, `}`, or an operator, found `bar`
--> src/main.rs:3:1
|
3 | #[foo_bar_attribute]
| ^^^^^^^^^^^^^^^^^^^^- help: try adding a comma: `,`
| |
| expected one of `,`, `.`, `?`, `}`, or an operator
4 | #[derive(Debug, Default)]
5 | struct MyStruct {
| -------- while parsing this struct
|
= note: this error originates in the attribute macro `foo_bar_attribute` (in Nightly builds, run with -Z macro-backtrace for more info)
Desired output
Rationale and extra context
i created a custom macro attribute but made a syntax error in the method that creates the struct with it's attribute. the error message is unhelpful which is a bit of surprise from Rust. it should correctly locate the point at which the it occurs
Other cases
Rust Version
rustc --version --verbose
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example from lib.rs and main.rs, focusing on the syntax error inside the generated Default implementation and the resulting diagnostic at the attribute. Trace how the compiler reports errors from the procedural macro, then verify that the diagnostic points to the generated syntax error or otherwise gives a more useful location than the attribute invocation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100