-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Use struct name as span instead of entire block #38328
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Instead of replacing the current spans, I could add a new |
Would be nice to point at the field which causes the recursion instead :) |
@nagisa That is definitely a part of #35965 that I've been looking at, but it'll take me some more time to actually clean up the code identifying the field from the detected That change is a bit more contained than this one. This PR will change the presentation of all places where a struct's span is used. |
I haven’t looked at the actual PR previously and only left a comment in passing. Now I see what you did here. It seems wrong. Firstly because when getting span of something (e.g. item) you expect the span to encompass the whole item, always. That’s universally true across compiler. For example, I imagine code generating debug info for structures and stuff may be using the full span and doing such change as yours would end up making the lines in debug info… weird. |
I’d probably just make |
``` error[E0072]: recursive type `ListNode` has infinite size --> ../../../src/test/compile-fail/E0072.rs:11:8 | 11 | struct ListNode { //~ ERROR E0072 | ^^^^^^^^ recursive type has infinite size | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `ListNode` representable ```
since the name-span is only useful in case of errors and the name span is a sub-span of the item span, we could instead write a function that uses an item span and extracts the name span from that by looking into the codemap and reparse the item span. |
7414ad6
to
0d19ec5
Compare
@nagisa I've followed that suggestion (in the naive way, there's lots of room to clean up the patch) of using @oli-obk I don't think there's any way to get the item name span reliably from the item span other than fully parsing the code therein, which feels to me like a worse solution that just storing that info in the first place. |
8192a6d
to
087155d
Compare
As per nagisa's review comment. WIP.
087155d
to
8a383b1
Compare
My take: I am 👍 on using the span for the item's name more generally -- both in this error but also in the errors that refer to unused methods and so forth. So making the item's name spanned seems like a good idea.
I agree that we can't automatically derive the name span from the other span. I guess you don't have to do a full parse -- in general you only have to skip the keyword ( macro_rules! mk_struct {
($n:ident) => {
struct $n { field: $n }
}
}
mk_struct!(Foo);
fn main() { } I've only skimmed the patch, so I'm not sure whether the various uses of |
(On the playground, that example cites the macro definition; I imagine that after your change it will ... probably? ... cite the macro use site.) |
☔ The latest upstream changes (presumably #38499) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
Not sure if this feels quite right. The dummy_spanned
calls worry me. What do you think about my suggestion? Perhaps there is a better way to avoid them?
@@ -798,7 +798,7 @@ impl<'a> LoweringContext<'a> { | |||
path.span = span; | |||
self.items.insert(import.id, hir::Item { | |||
id: import.id, | |||
name: import.rename.unwrap_or(ident).name, | |||
name: dummy_spanned(import.rename.unwrap_or(ident).name), |
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.
why dummy_spanned
here are not use span
?
@@ -227,7 +228,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt, | |||
|
|||
MacEager::items(SmallVector::many(vec![ | |||
P(ast::Item { | |||
ident: name.clone(), | |||
ident: codemap::dummy_spanned(name.clone()), |
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.
Hmm, why dummy_spanned
here? I would think span
would be a better choice.
@@ -1004,7 +1004,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { | |||
// FIXME: Would be nice if our generated code didn't violate | |||
// Rust coding conventions | |||
P(ast::Item { | |||
ident: name, | |||
ident: name.dummy_spanned(), |
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.
Similarly here. I feel like dummy_spanned
is basically always bad, since it implies that we can't just cite an error at item.name.span
, but rather we have to maybe fallback to item.span
.
This perhaps suggests that rather than having us do item.name.span
, we should add item.best_span
or something, which is the span to use in an error (versus being the span of the entire item in the source).
@estebank ping :) Thoughts on this?
|
@nikomatsakis, the There're four options as I see them:
|
@estebank I think I prefer one of the two final variants. I am not a big fan of trying to parse the span, but I could see it working fairly well in practice as a heuristic. I am nervous around macros. Otherwise, I think it might make sense to add a span field to (Alternatively, we could repurpose the existing I think my feeling is that we don't need to "specialize" to citing the name of an item -- we just want to know what is the best span to use when trying to locate "the item" itself. Sometimes this will be a name, but for some kinds of items it might be something different. For example, for an |
@estebank Are you planning to rework this substantially, then? I'm going to close this PR in the interim -- just to keep my queue tidy -- but feel free to re-open. |
@nikomatsakis go ahead. Is it ok if I set you as reviewer directly of the new PR? |
@estebank yep |
…nkov Add a way to get shorter spans until `char` for pointing at defs ```rust error[E0072]: recursive type `X` has infinite size --> file.rs:10:1 | 10 | struct X { | ^^^^^^^^ recursive type has infinite size | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `X` representable ``` vs ```rust error[E0072]: recursive type `X` has infinite size --> file.rs:10:1 | 10 | struct X { | _^ starting here... 11 | | x: X, 12 | | } | |_^ ...ending here: recursive type has infinite size | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `X` representable ``` Re: rust-lang#35965, rust-lang#38246. Follow up to rust-lang#38328. r? @jonathandturner
instead of
Re #35965, #38246.