Skip to content

Recover invalid return type until {, } or ; - #163059

Open
inkreasing wants to merge 4 commits into
rust-lang:mainfrom
inkreasing:fix162947-part2
Open

inkreasing wants to merge 4 commits into
rust-lang:mainfrom
inkreasing:fix162947-part2

Conversation

@inkreasing

@inkreasing inkreasing commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

View all comments

Fixes #162947 (this time completely i think)

r? estebank (since you suggested this fix)

No AI used.

@rustbot

rustbot commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 20, 2026
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
{
self.bump();
}
return Ok(FnRetTy::Default(self.prev_token.span));

@inkreasing inkreasing Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure about this span.
maybe this should be the complete return type that was skipped?

I don't think i can do anything other than make the return type (). It can lead to wrong type mismatches.

View changes since the review

@inkreasing inkreasing Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh i should probably make the type an err.
edit: done. still unsure about the span.

|
LL - fn foo<T>() where T: Default -> {
LL + fn foo<T>() -> where T: Default {
|

@inkreasing inkreasing Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very unsure why this calls the where bounds function parameters?
Otherwise the suggestion isn't too bad.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion now triggers since you've updated parse_ret_ty to return Ok(_) in this case and so in error_fn_body_not_found the code believes it can use the parsed return type to offer the suggestion "since everything went well". Previously, it noped outta there as it used to be an Err(_).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh i didn't even notice that parse_return_type was called if parsing of the function body failed. ty

@inkreasing
inkreasing force-pushed the fix162947-part2 branch 2 times, most recently from 966513a to aa112af Compare September 20, 2026 08:28
Comment thread tests/ui/const-generics/ice-const-generic-function-return-ty.rs Outdated
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
Comment thread tests/ui/parser/issues/invalid-ret-ty-issue-162947.rs
Comment on lines +15 to +16
LL - fn foo<T>() where T: Default -> {
LL + fn foo<T>() -> where T: Default {

@fmease fmease Sep 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion needs to be fixed since it's obviously incorrect: The "insertion span" probably needs to include the span of the where clause if available. Not sure if feasible.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it obviously incorrect? i mean that suggested code doesn't compile, yes, but moving the return arrow before the where clause is correct.
Maybe i can try to insert something like a {return_type} into the suggestion after the arrow if there is a where?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i add a incorrect return type the suggestion looks a lot better.
fn foo<T>() where T: Default -> 1 + 1 {

help: place the return type after the function parameters
   |
LL - fn foo<T>() where T: Default -> 1 + 1 {
LL + fn foo<T>() -> 1 + 1 where T: Default  {
   |

|
LL - fn foo<T>() where T: Default -> {
LL + fn foo<T>() -> where T: Default {
|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion now triggers since you've updated parse_ret_ty to return Ok(_) in this case and so in error_fn_body_not_found the code believes it can use the parsed return type to offer the suggestion "since everything went well". Previously, it noped outta there as it used to be an Err(_).

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 20, 2026
@inkreasing inkreasing changed the title Recover invalid return type unitl {, } or ; Recover invalid return type until {, } or ; Sep 20, 2026
@inkreasing

Copy link
Copy Markdown
Contributor Author

@rustbot ready

Either addressed or responded to the comments.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2026
--> $DIR/invalid-ret-ty-issue-162947.rs:13:15
|
LL | fn a() -> 1 + 1 {
| ^ expected type

@inkreasing inkreasing Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally i would like to change the span to point to the whole invalid return type.
I think for that i would need to edit the span of the Diag after it was created, but i don't know if that is even possible.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are APIs to modify the Diag's state, but it is generally safer to create a new one to emit, and delay_as_bug the previous one when you're significantly changing the error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought about this and i don't think it is really possible (at least not without also changing the type parser).
I don't think i can differentiate between the -> 1 + 1 { and the -> A<B { error, but i would need to, since i only want to do that span expansion for the first case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keyword as a return type doesn't silence "no method found" errors

4 participants