-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Implement a explicit_generic_args_with_impl_trait
feature gate
#86176
Merged
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -459,7 +459,32 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
|
||
let default_counts = gen_params.own_defaults(); | ||
let param_counts = gen_params.own_counts(); | ||
let named_type_param_count = param_counts.types - has_self as usize; | ||
|
||
// Subtracting from param count to ensure type params synthesized from `impl Trait` | ||
// cannot be explictly specified even with `explicit_generic_args_with_impl_trait` | ||
// feature enabled. | ||
let synth_type_param_count = if tcx.features().explicit_generic_args_with_impl_trait { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like a small comment here just explaining what we're doing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
gen_params | ||
.params | ||
.iter() | ||
.filter(|param| { | ||
matches!( | ||
param.kind, | ||
ty::GenericParamDefKind::Type { | ||
synthetic: Some( | ||
hir::SyntheticTyParamKind::ImplTrait | ||
| hir::SyntheticTyParamKind::FromAttr | ||
), | ||
.. | ||
} | ||
) | ||
}) | ||
.count() | ||
} else { | ||
0 | ||
}; | ||
let named_type_param_count = | ||
param_counts.types - has_self as usize - synth_type_param_count; | ||
let infer_lifetimes = | ||
gen_pos != GenericArgPosition::Type && !gen_args.has_lifetime_params(); | ||
|
||
|
@@ -588,6 +613,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
param_counts.consts + named_type_param_count | ||
- default_counts.types | ||
- default_counts.consts | ||
- synth_type_param_count | ||
}; | ||
debug!("expected_min: {:?}", expected_min); | ||
debug!("arg_counts.lifetimes: {:?}", gen_args.num_lifetime_params()); | ||
|
@@ -617,7 +643,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
seg: &hir::PathSegment<'_>, | ||
generics: &ty::Generics, | ||
) -> bool { | ||
let explicit = !seg.infer_args; | ||
if seg.infer_args || tcx.features().explicit_generic_args_with_impl_trait { | ||
return false; | ||
} | ||
|
||
let impl_trait = generics.params.iter().any(|param| { | ||
matches!( | ||
param.kind, | ||
|
@@ -630,7 +659,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
) | ||
}); | ||
|
||
if explicit && impl_trait { | ||
if impl_trait { | ||
let spans = seg | ||
.args() | ||
.args | ||
|
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`. |
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.
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.
I suppose we can just co-opt that issue to be a tracking issue, yeah.