-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Suggest function-local constructors without enclosing function path #156889
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
Merged
rust-bors
merged 1 commit into
rust-lang:main
from
onehr:fix-local-tuple-constructor-suggestion-144319
May 28, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/ui/suggestions/wrap-function-local-constructors.fixed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| //@ run-rustfix | ||
|
|
||
| // Regression test for https://github.com/rust-lang/rust/issues/144319. | ||
| // Function-local constructors cannot be named through the enclosing function | ||
| // path. The suggestion must omit path segments that cannot be written in source, | ||
| // while preserving real path segments like local modules and enums. | ||
|
|
||
| #![allow(dead_code)] | ||
|
|
||
| fn direct_tuple_struct() { | ||
| struct Foo(bool); | ||
| struct Bar(Foo); | ||
|
|
||
| _ = Bar(Foo(false)); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `Foo` | ||
| } | ||
|
|
||
| fn enum_variant() { | ||
| enum LocalResult<T> { | ||
| Ok(T), | ||
| } | ||
| struct Bar(LocalResult<bool>); | ||
|
|
||
| _ = Bar(LocalResult::Ok(false)); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `LocalResult::Ok` | ||
| } | ||
|
|
||
| fn local_module() { | ||
| mod inner { | ||
| pub struct Foo(pub bool); | ||
| } | ||
| struct Bar(inner::Foo); | ||
|
|
||
| _ = Bar(inner::Foo(false)); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `inner::Foo` | ||
| } | ||
|
|
||
| fn closure_body() { | ||
| let _ = || { | ||
| struct Foo(bool); | ||
| struct Bar(Foo); | ||
|
|
||
| _ = Bar(Foo(false)); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `Foo` | ||
| }; | ||
| } | ||
|
|
||
| fn inline_const_block() { | ||
| const { | ||
| struct Foo(bool); | ||
| struct Bar(Foo); | ||
|
|
||
| _ = Bar(Foo(false)); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `Foo` | ||
| }; | ||
| } | ||
|
|
||
| pub fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| //@ run-rustfix | ||
|
|
||
| // Regression test for https://github.com/rust-lang/rust/issues/144319. | ||
| // Function-local constructors cannot be named through the enclosing function | ||
| // path. The suggestion must omit path segments that cannot be written in source, | ||
| // while preserving real path segments like local modules and enums. | ||
|
|
||
| #![allow(dead_code)] | ||
|
|
||
| fn direct_tuple_struct() { | ||
| struct Foo(bool); | ||
| struct Bar(Foo); | ||
|
|
||
| _ = Bar(false); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `Foo` | ||
| } | ||
|
|
||
| fn enum_variant() { | ||
| enum LocalResult<T> { | ||
| Ok(T), | ||
| } | ||
| struct Bar(LocalResult<bool>); | ||
|
|
||
| _ = Bar(false); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `LocalResult::Ok` | ||
| } | ||
|
|
||
| fn local_module() { | ||
| mod inner { | ||
| pub struct Foo(pub bool); | ||
| } | ||
| struct Bar(inner::Foo); | ||
|
|
||
| _ = Bar(false); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `inner::Foo` | ||
| } | ||
|
|
||
| fn closure_body() { | ||
| let _ = || { | ||
| struct Foo(bool); | ||
| struct Bar(Foo); | ||
|
|
||
| _ = Bar(false); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `Foo` | ||
| }; | ||
| } | ||
|
|
||
| fn inline_const_block() { | ||
| const { | ||
| struct Foo(bool); | ||
| struct Bar(Foo); | ||
|
|
||
| _ = Bar(false); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP try wrapping the expression in `Foo` | ||
| }; | ||
| } | ||
|
|
||
| pub fn main() {} |
95 changes: 95 additions & 0 deletions
95
tests/ui/suggestions/wrap-function-local-constructors.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| error[E0308]: mismatched types | ||
| --> $DIR/wrap-function-local-constructors.rs:14:13 | ||
| | | ||
| LL | _ = Bar(false); | ||
| | --- ^^^^^ expected `Foo`, found `bool` | ||
| | | | ||
| | arguments to this struct are incorrect | ||
| | | ||
| note: tuple struct defined here | ||
| --> $DIR/wrap-function-local-constructors.rs:12:12 | ||
| | | ||
| LL | struct Bar(Foo); | ||
| | ^^^ | ||
| help: try wrapping the expression in `Foo` | ||
| | | ||
| LL | _ = Bar(Foo(false)); | ||
| | ++++ + | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/wrap-function-local-constructors.rs:25:13 | ||
| | | ||
| LL | _ = Bar(false); | ||
| | --- ^^^^^ expected `LocalResult<bool>`, found `bool` | ||
| | | | ||
| | arguments to this struct are incorrect | ||
| | | ||
| = note: expected enum `LocalResult<bool>` | ||
| found type `bool` | ||
| note: tuple struct defined here | ||
| --> $DIR/wrap-function-local-constructors.rs:23:12 | ||
| | | ||
| LL | struct Bar(LocalResult<bool>); | ||
| | ^^^ | ||
| help: try wrapping the expression in `LocalResult::Ok` | ||
| | | ||
| LL | _ = Bar(LocalResult::Ok(false)); | ||
| | ++++++++++++++++ + | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/wrap-function-local-constructors.rs:36:13 | ||
| | | ||
| LL | _ = Bar(false); | ||
| | --- ^^^^^ expected `Foo`, found `bool` | ||
| | | | ||
| | arguments to this struct are incorrect | ||
| | | ||
| note: tuple struct defined here | ||
| --> $DIR/wrap-function-local-constructors.rs:34:12 | ||
| | | ||
| LL | struct Bar(inner::Foo); | ||
| | ^^^ | ||
| help: try wrapping the expression in `inner::Foo` | ||
| | | ||
| LL | _ = Bar(inner::Foo(false)); | ||
| | +++++++++++ + | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/wrap-function-local-constructors.rs:46:17 | ||
| | | ||
| LL | _ = Bar(false); | ||
| | --- ^^^^^ expected `Foo`, found `bool` | ||
| | | | ||
| | arguments to this struct are incorrect | ||
| | | ||
| note: tuple struct defined here | ||
| --> $DIR/wrap-function-local-constructors.rs:44:16 | ||
| | | ||
| LL | struct Bar(Foo); | ||
| | ^^^ | ||
| help: try wrapping the expression in `Foo` | ||
| | | ||
| LL | _ = Bar(Foo(false)); | ||
| | ++++ + | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/wrap-function-local-constructors.rs:57:17 | ||
| | | ||
| LL | _ = Bar(false); | ||
| | --- ^^^^^ expected `Foo`, found `bool` | ||
| | | | ||
| | arguments to this struct are incorrect | ||
| | | ||
| note: tuple struct defined here | ||
| --> $DIR/wrap-function-local-constructors.rs:55:16 | ||
| | | ||
| LL | struct Bar(Foo); | ||
| | ^^^ | ||
| help: try wrapping the expression in `Foo` | ||
| | | ||
| LL | _ = Bar(Foo(false)); | ||
| | ++++ + | ||
|
|
||
| error: aborting due to 5 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0308`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.