forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#86176 - nbdd0121:explicit-generic-args, r=j…
…ackh726 Implement a `explicit_generic_args_with_impl_trait` feature gate Implements rust-lang#83701 When this gate is enabled, explicit generic arguments can be specified even if `impl Trait` is used in argument position. Generic arguments can only be specified for explicit generic parameters but not for the synthetic type parameters from `impl Trait` So code like this will be accepted: ```rust #![feature(explicit_generic_args_with_impl_trait)] fn foo<T: ?Sized>(_f: impl AsRef<T>) {} fn main() { foo::<str>("".to_string()); } ```
- Loading branch information
Showing
10 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
53 changes: 53 additions & 0 deletions
53
...oc/unstable-book/src/language-features/explicit-generic-args-with-impl-trait.md
This file contains 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,53 @@ | ||
# `explicit_generic_args_with_impl_trait` | ||
|
||
The tracking issue for this feature is: [#83701] | ||
|
||
[#83701]: https://github.com/rust-lang/rust/issues/83701 | ||
|
||
------------------------ | ||
|
||
The `explicit_generic_args_with_impl_trait` feature gate lets you specify generic arguments even | ||
when `impl Trait` is used in argument position. | ||
|
||
A simple example is: | ||
|
||
```rust | ||
#![feature(explicit_generic_args_with_impl_trait)] | ||
|
||
fn foo<T: ?Sized>(_f: impl AsRef<T>) {} | ||
|
||
fn main() { | ||
foo::<str>("".to_string()); | ||
} | ||
``` | ||
|
||
This is currently rejected: | ||
|
||
```text | ||
error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position | ||
--> src/main.rs:6:11 | ||
| | ||
6 | foo::<str>("".to_string()); | ||
| ^^^ explicit generic argument not allowed | ||
``` | ||
|
||
However it would compile if `explicit_generic_args_with_impl_trait` is enabled. | ||
|
||
Note that the synthetic type parameters from `impl Trait` are still implicit and you | ||
cannot explicitly specify these: | ||
|
||
```rust,compile_fail | ||
#![feature(explicit_generic_args_with_impl_trait)] | ||
fn foo<T: ?Sized>(_f: impl AsRef<T>) {} | ||
fn bar<T: ?Sized, F: AsRef<T>>(_f: F) {} | ||
fn main() { | ||
bar::<str, _>("".to_string()); // Okay | ||
bar::<str, String>("".to_string()); // Okay | ||
foo::<str>("".to_string()); // Okay | ||
foo::<str, String>("".to_string()); // Error, you cannot specify `impl Trait` explicitly | ||
} | ||
``` |
7 changes: 7 additions & 0 deletions
7
...est/ui/impl-trait/explicit-generic-args-with-impl-trait/explicit-generic-args-for-impl.rs
This file contains 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,7 @@ | ||
#![feature(explicit_generic_args_with_impl_trait)] | ||
|
||
fn foo<T: ?Sized>(_f: impl AsRef<T>) {} | ||
|
||
fn main() { | ||
foo::<str, String>("".to_string()); //~ ERROR E0107 | ||
} |
17 changes: 17 additions & 0 deletions
17
...ui/impl-trait/explicit-generic-args-with-impl-trait/explicit-generic-args-for-impl.stderr
This file contains 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,17 @@ | ||
error[E0107]: this function takes at most 1 generic argument but 2 generic arguments were supplied | ||
--> $DIR/explicit-generic-args-for-impl.rs:6:5 | ||
| | ||
LL | foo::<str, String>("".to_string()); | ||
| ^^^ ------ help: remove this generic argument | ||
| | | ||
| expected at most 1 generic argument | ||
| | ||
note: function defined here, with at most 1 generic parameter: `T` | ||
--> $DIR/explicit-generic-args-for-impl.rs:3:4 | ||
| | ||
LL | fn foo<T: ?Sized>(_f: impl AsRef<T>) {} | ||
| ^^^ - | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0107`. |
9 changes: 9 additions & 0 deletions
9
src/test/ui/impl-trait/explicit-generic-args-with-impl-trait/explicit-generic-args.rs
This file contains 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,9 @@ | ||
// check-pass | ||
|
||
#![feature(explicit_generic_args_with_impl_trait)] | ||
|
||
fn foo<T: ?Sized>(_f: impl AsRef<T>) {} | ||
|
||
fn main() { | ||
foo::<str>("".to_string()); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/test/ui/impl-trait/explicit-generic-args-with-impl-trait/feature-gate.rs
This file contains 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,7 @@ | ||
// gate-test-explicit_generic_args_with_impl_trait | ||
|
||
fn foo<T: ?Sized>(_f: impl AsRef<T>) {} | ||
|
||
fn main() { | ||
foo::<str>("".to_string()); //~ ERROR E0632 | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/ui/impl-trait/explicit-generic-args-with-impl-trait/feature-gate.stderr
This file contains 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,9 @@ | ||
error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position | ||
--> $DIR/feature-gate.rs:6:11 | ||
| | ||
LL | foo::<str>("".to_string()); | ||
| ^^^ explicit generic argument not allowed | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0632`. |