Skip to content

Commit ac7651c

Browse files
More polishing
1 parent 52c6b10 commit ac7651c

File tree

12 files changed

+79
-5
lines changed

12 files changed

+79
-5
lines changed

compiler/rustc_hir/src/hir.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,11 @@ pub enum PreciseCapturingArg<'hir> {
25682568
Param(PreciseCapturingNonLifetimeArg),
25692569
}
25702570

2571+
/// We need to have a [`Node`] for the [`HirId`] that we attach the type/const param
2572+
/// resolution to. Lifetimes don't have this problem, and for them, it's actually
2573+
/// kind of detrimental to use a custom node type versus just using [`Lifetime`],
2574+
/// since resolve_bound_vars operates on `Lifetime`s.
2575+
// FIXME(precise_capturing): Investigate storing this as a path instead?
25712576
#[derive(Debug, Clone, Copy, HashStable_Generic)]
25722577
pub struct PreciseCapturingNonLifetimeArg {
25732578
pub hir_id: HirId,

compiler/rustc_hir_analysis/messages.ftl

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ hir_analysis_auto_deref_reached_recursion_limit = reached the recursion limit wh
3939
4040
hir_analysis_bad_precise_capture = expected {$kind} parameter in `use<...>` precise captures list, found {$found}
4141
42-
hir_analysis_precise_capture_self_alias = `Self` can't be captured in `use<...>` precise captures list, since it is an alias
43-
.label = `Self` is not a generic argument, but an alias to the type of the {$what}
44-
4542
hir_analysis_cannot_capture_late_bound_const =
4643
cannot capture late-bound const parameter in {$what}
4744
.label = parameter defined here
@@ -374,6 +371,9 @@ hir_analysis_pattern_type_wild_pat = "wildcard patterns are not permitted for pa
374371
hir_analysis_placeholder_not_allowed_item_signatures = the placeholder `_` is not allowed within types on item signatures for {$kind}
375372
.label = not allowed in type signatures
376373
374+
hir_analysis_precise_capture_self_alias = `Self` can't be captured in `use<...>` precise captures list, since it is an alias
375+
.label = `Self` is not a generic argument, but an alias to the type of the {$what}
376+
377377
hir_analysis_redundant_lifetime_args = unnecessary lifetime parameter `{$victim}`
378378
.note = you can use the `{$candidate}` lifetime directly, in place of `{$victim}`
379379

compiler/rustc_hir_analysis/src/check/check.rs

+8
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,14 @@ fn sanity_check_found_hidden_type<'tcx>(
475475
}
476476
}
477477

478+
/// Check that the opaque's precise captures list is valid (if present).
479+
/// We check this for regular `impl Trait`s and also RPITITs, even though the latter
480+
/// are technically GATs.
481+
///
482+
/// This function is responsible for:
483+
/// 1. Checking that all type/const params are mention in the captures list.
484+
/// 2. Checking that all lifetimes that are implicitly captured are mentioned.
485+
/// 3. Asserting that all parameters mentioned in the captures list are invariant.
478486
fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDefId) {
479487
let hir::OpaqueTy { precise_capturing_args, .. } =
480488
*tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+1
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ pub fn suggest_new_region_bound(
283283
continue;
284284
}
285285
match fn_return.kind {
286+
// FIXME(precise_captures): Suggest adding to `use<...>` list instead.
286287
TyKind::OpaqueDef(item_id, _, _) => {
287288
let item = tcx.hir().item(item_id);
288289
let ItemKind::OpaqueTy(opaque) = &item.kind else {

compiler/rustc_parse/src/parser/ty.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,9 @@ impl<'a> Parser<'a> {
669669
})
670670
}
671671

672-
// parse precise captures, if any.
672+
// parse precise captures, if any. This is `use<'lt, 'lt, P, P>`; a list of
673+
// lifetimes and ident params (including SelfUpper). These are validated later
674+
// for order, duplication, and whether they actually reference params.
673675
let precise_capturing = if self.eat_keyword(kw::Use) {
674676
let use_span = self.prev_token.span;
675677
self.psess.gated_spans.gate(sym::precise_capturing, use_span);

compiler/rustc_resolve/src/late.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,12 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
10591059
PreciseCapturingArg::Lifetime(_) => {}
10601060

10611061
PreciseCapturingArg::Arg(path, id) => {
1062+
// we want `impl use<C>` to try to resolve `C` as both a type parameter or
1063+
// a const parameter. Since the resolver specifically doesn't allow having
1064+
// two generic params with the same name, even if they're a different namespace,
1065+
// it doesn't really matter which we try resolving first, but just like
1066+
// `Ty::Param` we just fall back to the value namespace only if it's missing
1067+
// from the type namespace.
10621068
let mut check_ns = |ns| {
10631069
self.maybe_resolve_ident_in_lexical_scope(path.segments[0].ident, ns).is_some()
10641070
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(precise_capturing)]
2+
//~^ WARN the feature `precise_capturing` is incomplete
3+
4+
fn hello(_: impl use<> Sized) {}
5+
//~^ ERROR `use<...>` precise capturing syntax not allowed on argument-position `impl Trait`
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: the feature `precise_capturing` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/apit.rs:1:12
3+
|
4+
LL | #![feature(precise_capturing)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error: `use<...>` precise capturing syntax not allowed on argument-position `impl Trait`
11+
--> $DIR/apit.rs:4:18
12+
|
13+
LL | fn hello(_: impl use<> Sized) {}
14+
| ^^^
15+
16+
error: aborting due to 1 previous error; 1 warning emitted
17+

tests/ui/impl-trait/precise-capturing/bad-params.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ impl MyType {
1313
//~^ ERROR `Self` can't be captured in `use<...>` precise captures list, since it is an alias
1414
}
1515

16+
fn hello() -> impl use<hello> Sized {}
17+
//~^ ERROR expected type or const parameter in `use<...>` precise captures list, found function
18+
1619
fn main() {}

tests/ui/impl-trait/precise-capturing/bad-params.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ LL | impl MyType {
3434
LL | fn self_is_not_param() -> impl use<Self> Sized {}
3535
| ^^^^
3636

37-
error: aborting due to 3 previous errors; 1 warning emitted
37+
error: expected type or const parameter in `use<...>` precise captures list, found function
38+
--> $DIR/bad-params.rs:16:24
39+
|
40+
LL | fn hello() -> impl use<hello> Sized {}
41+
| ^^^^^
42+
43+
error: aborting due to 4 previous errors; 1 warning emitted
3844

3945
Some errors have detailed explanations: E0411, E0412.
4046
For more information about an error, try `rustc --explain E0411`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ check-pass
2+
3+
#![feature(precise_capturing)]
4+
//~^ WARN the feature `precise_capturing` is incomplete
5+
6+
fn elided(x: &()) -> impl use<'_> Sized { x }
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `precise_capturing` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/elided.rs:3:12
3+
|
4+
LL | #![feature(precise_capturing)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+

0 commit comments

Comments
 (0)