Skip to content

Commit d801f0c

Browse files
Warn against redundant use<...>
1 parent eb866ff commit d801f0c

File tree

10 files changed

+272
-114
lines changed

10 files changed

+272
-114
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14041404
bounds,
14051405
fn_kind,
14061406
itctx,
1407-
precise_capturing.as_deref().map(|(args, _)| args.as_slice()),
1407+
precise_capturing.as_deref().map(|(args, span)| (args.as_slice(), *span)),
14081408
),
14091409
ImplTraitContext::Universal => {
14101410
if let Some(&(_, span)) = precise_capturing.as_deref() {
@@ -1520,7 +1520,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15201520
bounds: &GenericBounds,
15211521
fn_kind: Option<FnDeclKind>,
15221522
itctx: ImplTraitContext,
1523-
precise_capturing_args: Option<&[PreciseCapturingArg]>,
1523+
precise_capturing_args: Option<(&[PreciseCapturingArg], Span)>,
15241524
) -> hir::TyKind<'hir> {
15251525
// Make sure we know that some funky desugaring has been going on here.
15261526
// This is a first: there is code in other places like for loop
@@ -1530,7 +1530,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15301530
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
15311531

15321532
let captured_lifetimes_to_duplicate =
1533-
if let Some(precise_capturing) = precise_capturing_args {
1533+
if let Some((precise_capturing, _)) = precise_capturing_args {
15341534
// We'll actually validate these later on; all we need is the list of
15351535
// lifetimes to duplicate during this portion of lowering.
15361536
precise_capturing
@@ -1604,7 +1604,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16041604
captured_lifetimes_to_duplicate: FxIndexSet<Lifetime>,
16051605
span: Span,
16061606
opaque_ty_span: Span,
1607-
precise_capturing_args: Option<&[PreciseCapturingArg]>,
1607+
precise_capturing_args: Option<(&[PreciseCapturingArg], Span)>,
16081608
lower_item_bounds: impl FnOnce(&mut Self) -> &'hir [hir::GenericBound<'hir>],
16091609
) -> hir::TyKind<'hir> {
16101610
let opaque_ty_def_id = self.create_def(
@@ -1695,8 +1695,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16951695
this.with_remapping(captured_to_synthesized_mapping, |this| {
16961696
(
16971697
lower_item_bounds(this),
1698-
precise_capturing_args.map(|precise_capturing| {
1699-
this.lower_precise_capturing_args(precise_capturing)
1698+
precise_capturing_args.map(|(precise_capturing, span)| {
1699+
(
1700+
this.lower_precise_capturing_args(precise_capturing),
1701+
this.lower_span(span),
1702+
)
17001703
}),
17011704
)
17021705
});

compiler/rustc_hir/src/hir.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2631,7 +2631,7 @@ pub struct OpaqueTy<'hir> {
26312631
/// lowered as an associated type.
26322632
pub in_trait: bool,
26332633
/// List of arguments captured via `impl use<'a, P, ...> Trait` syntax.
2634-
pub precise_capturing_args: Option<&'hir [PreciseCapturingArg<'hir>]>,
2634+
pub precise_capturing_args: Option<(&'hir [PreciseCapturingArg<'hir>], Span)>,
26352635
}
26362636

26372637
#[derive(Debug, Clone, Copy, HashStable_Generic)]
@@ -2641,6 +2641,15 @@ pub enum PreciseCapturingArg<'hir> {
26412641
Param(PreciseCapturingNonLifetimeArg),
26422642
}
26432643

2644+
impl PreciseCapturingArg<'_> {
2645+
pub fn hir_id(self) -> HirId {
2646+
match self {
2647+
PreciseCapturingArg::Lifetime(lt) => lt.hir_id,
2648+
PreciseCapturingArg::Param(param) => param.hir_id,
2649+
}
2650+
}
2651+
}
2652+
26442653
/// We need to have a [`Node`] for the [`HirId`] that we attach the type/const param
26452654
/// resolution to. Lifetimes don't have this problem, and for them, it's actually
26462655
/// kind of detrimental to use a custom node type versus just using [`Lifetime`],

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
533533
try_visit!(visitor.visit_id(item.hir_id()));
534534
try_visit!(walk_generics(visitor, generics));
535535
walk_list!(visitor, visit_param_bound, bounds);
536-
if let Some(precise_capturing_args) = precise_capturing_args {
536+
if let Some((precise_capturing_args, _)) = precise_capturing_args {
537537
for arg in precise_capturing_args {
538538
try_visit!(visitor.visit_precise_capturing_arg(arg));
539539
}

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ fn sanity_check_found_hidden_type<'tcx>(
485485
fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDefId) {
486486
let hir::OpaqueTy { precise_capturing_args, .. } =
487487
*tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
488-
let Some(precise_capturing_args) = precise_capturing_args else {
488+
let Some((precise_capturing_args, _)) = precise_capturing_args else {
489489
// No precise capturing args; nothing to validate
490490
return;
491491
};

compiler/rustc_lint/messages.ftl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ lint_array_into_iter =
99
.use_explicit_into_iter_suggestion =
1010
or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
1111
12-
lint_impl_trait_overcaptures = `{$self_ty}` will capture more lifetimes than possibly intended in edition 2024
13-
.note = specifically, {$num_captured ->
14-
[one] this lifetime is
15-
*[other] these lifetimes are
16-
} in scope but not mentioned in the type's bounds
17-
.note2 = all lifetimes in scope will be captured by `impl Trait`s in edition 2024
18-
.suggestion = use the precise capturing `use<...>` syntax to make the captures explicit
19-
2012
lint_async_fn_in_trait = use of `async fn` in public traits is discouraged as auto trait bounds cannot be specified
2113
.note = you can suppress this lint if you plan to use the trait only in your own code, or do not care about auto traits like `Send` on the `Future`
2214
.suggestion = you can alternatively desugar to a normal `fn` that returns `impl Future` and add any desired bounds such as `Send`, but these cannot be relaxed without a breaking API change
@@ -277,6 +269,17 @@ lint_identifier_uncommon_codepoints = identifier contains {$codepoints_len ->
277269
278270
lint_ignored_unless_crate_specified = {$level}({$name}) is ignored unless specified at crate level
279271
272+
lint_impl_trait_overcaptures = `{$self_ty}` will capture more lifetimes than possibly intended in edition 2024
273+
.note = specifically, {$num_captured ->
274+
[one] this lifetime is
275+
*[other] these lifetimes are
276+
} in scope but not mentioned in the type's bounds
277+
.note2 = all lifetimes in scope will be captured by `impl Trait`s in edition 2024
278+
.suggestion = use the precise capturing `use<...>` syntax to make the captures explicit
279+
280+
lint_impl_trait_redundant_captures = all possible in-scope parameters are already captured, so `use<...>` syntax is redundant
281+
.suggestion = remove the `use<...>` syntax
282+
280283
lint_improper_ctypes = `extern` {$desc} uses type `{$ty}`, which is not FFI-safe
281284
.label = not FFI-safe
282285
.note = the type is defined here

0 commit comments

Comments
 (0)