give some suggestion for returning anonymous enum#100988
give some suggestion for returning anonymous enum#100988LYF1999 wants to merge 1 commit intorust-lang:masterfrom
Conversation
|
should we create a crate for some util functions like |
f615844 to
ca90121
Compare
This comment has been minimized.
This comment has been minimized.
ca90121 to
d436357
Compare
d436357 to
bac60db
Compare
| "SomeEnum", | ||
| Applicability::HasPlaceholders, | ||
| ) | ||
| .note(suggestion_code); |
There was a problem hiding this comment.
Instead of the note with the enum, we could instead use a multipart_suggestion and pass in the span for the space right at the start of the item, so that the output can be
error: anonymous enums are not supported
--> $DIR/issue-100741-return-anonymous-enum.rs:3:17
|
LL | fn foo<'a>() -> i32 | Vec<i32> | &str | &'a String | Foo {
| ^ ^ ^ ^ not supported in types
|
help: consider using enum as return type
|
LL + enum SomeEnum<'lifetime,'a>{
LL + I32(i32)
LL + Vec(Vec<i32>)
LL + StrRef(&'lifetime str)
LL + StringRef(&'a String)
LL + Foo(Foo)
LL + }
LL ~ fn foo<'a>() -> SomeEnum<'_, 'a> {
|
| err.span_suggestion( | ||
| span, | ||
| "consider using enum as return type", | ||
| "SomeEnum", |
There was a problem hiding this comment.
You need to account for the lifetimes that were mentioned. In the test included, this would end up being '_, 'a.
|
|
||
| if let Some(_args) = &seg.args { | ||
| ty_string.push('<'); | ||
| ty_string.push_str("..."); |
There was a problem hiding this comment.
Why don't we print the whole type?
There was a problem hiding this comment.
it's more complicated, for example Vec<impl Send> | Vec<(i32, i64)>. We can not make a enum variant name for these type.
There was a problem hiding this comment.
As you propose, we can use Variant{N} as the variant name
| let mut ty_string = String::new(); | ||
|
|
||
| if let Some(seg) = path.segments.iter().last() { | ||
| name.push_str(seg.ident.name.as_str()); |
There was a problem hiding this comment.
It feels like the only thing we need here is the name and for ty_string format!("{ty}") is enough, after folding the anon lifetimes to be named as 'lifetime, in way similar to what is done here:
rust/compiler/rustc_middle/src/ty/erase_regions.rs
Lines 56 to 69 in c3fce8e
There was a problem hiding this comment.
emmm, the TyKind in rustc_ast doesn't implement Display, if it's nesscery to implement Display, I will submit a new pull request to do that.
There was a problem hiding this comment.
I'm fairly certain there's a way to pprint TyKind, but it isn't through Display directly 🤔
| None | ||
| } | ||
| } | ||
| _ => None, |
There was a problem hiding this comment.
This wouldn't work for something like -> [i32; 10] | Vec<i32>, right?
If you don't want to support every TyKind, you can at least provide a Variant{N} name so that the suggested enum makes syntactic sense.
| let mut tys = vec![]; | ||
| let lo = self.token.span; | ||
| loop { |
There was a problem hiding this comment.
The issue with this that we shouldn't recover this only for return types, we should be doing this further down, in parse_ty_common, maybe adding another argument to it to explicitly state what should be coming after it (to support things like let foo: A | B = ...;).
| u.next().is_some() | ||
| } | ||
|
|
||
| pub fn to_camel_case(s: &str) -> String { |
There was a problem hiding this comment.
There was a problem hiding this comment.
but it's in crate rustc_lint. I think it's weird to make rustc_parse depend rustc_lint. Should we create a crate rustc_utils for these functions like to_camel_case
There was a problem hiding this comment.
We could move this to a shared dependency of the two, like rustc_errors.
|
☔ The latest upstream changes (presumably #107314) made this pull request unmergeable. Please resolve the merge conflicts. |
|
any updates here? we have to be very careful to not recover on the happy path as to not cause further regressions |
|
sorry, I am not working on it currently |
|
☔ The latest upstream changes (presumably #109128) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Closing this as inactive. Feel free to reöpen this pr or create a new pr if you get the time to work on this. Thanks |
give some suggestion for returning anonymous enum
fix #100741
r? @estebank