Skip to content

Commit fa0428c

Browse files
Flip spans for precise capturing syntax not capturing a ty/ct param
1 parent 584f183 commit fa0428c

File tree

7 files changed

+37
-28
lines changed

7 files changed

+37
-28
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -580,27 +580,34 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
580580

581581
match param.kind {
582582
ty::GenericParamDefKind::Lifetime => {
583+
let use_span = tcx.def_span(param.def_id);
584+
let opaque_span = tcx.def_span(opaque_def_id);
583585
// Check if the lifetime param was captured but isn't named in the precise captures list.
584586
if variances[param.index as usize] == ty::Invariant {
585-
let param_span = if let DefKind::OpaqueTy =
586-
tcx.def_kind(tcx.parent(param.def_id))
587+
if let DefKind::OpaqueTy = tcx.def_kind(tcx.parent(param.def_id))
587588
&& let ty::ReEarlyParam(ty::EarlyParamRegion { def_id, .. })
588589
| ty::ReLateParam(ty::LateParamRegion {
589590
bound_region: ty::BoundRegionKind::BrNamed(def_id, _),
590591
..
591592
}) = *tcx
592593
.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local())
593594
{
594-
Some(tcx.def_span(def_id))
595+
tcx.dcx().emit_err(errors::LifetimeNotCaptured {
596+
opaque_span,
597+
use_span,
598+
param_span: tcx.def_span(def_id),
599+
});
595600
} else {
596-
None
597-
};
598-
// FIXME(precise_capturing): Structured suggestion for this would be useful
599-
tcx.dcx().emit_err(errors::LifetimeNotCaptured {
600-
use_span: tcx.def_span(param.def_id),
601-
param_span,
602-
opaque_span: tcx.def_span(opaque_def_id),
603-
});
601+
// If the `use_span` is actually just the param itself, then we must
602+
// have not duplicated the lifetime but captured the original.
603+
// The "effective" `use_span` will be the span of the opaque itself,
604+
// and the param span will be the def span of the param.
605+
tcx.dcx().emit_err(errors::LifetimeNotCaptured {
606+
opaque_span,
607+
use_span: opaque_span,
608+
param_span: use_span,
609+
});
610+
}
604611
continue;
605612
}
606613
}

compiler/rustc_hir_analysis/src/errors/precise_captures.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use rustc_span::{Span, Symbol};
66
#[note]
77
pub struct ParamNotCaptured {
88
#[primary_span]
9-
pub param_span: Span,
10-
#[label]
119
pub opaque_span: Span,
10+
#[label]
11+
pub param_span: Span,
1212
pub kind: &'static str,
1313
}
1414

@@ -18,7 +18,7 @@ pub struct LifetimeNotCaptured {
1818
#[primary_span]
1919
pub use_span: Span,
2020
#[label(hir_analysis_param_label)]
21-
pub param_span: Option<Span>,
21+
pub param_span: Span,
2222
#[label]
2323
pub opaque_span: Span,
2424
}

tests/ui/impl-trait/precise-capturing/capture-parent-arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ impl<'a> W<'a> {
3131

3232
// But also make sure that we error here...
3333
impl<'a> W<'a> {
34-
//~^ ERROR `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
3534
fn bad2() -> impl use<> Into<<Self as Tr>::Assoc> {}
35+
//~^ ERROR `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
3636
}
3737

3838
fn main() {}

tests/ui/impl-trait/precise-capturing/capture-parent-arg.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ LL | fn bad1() -> impl use<> Into<<W<'a> as Tr>::Assoc> {}
1616
| -------------------^^---------------- lifetime captured due to being mentioned in the bounds of the `impl Trait`
1717

1818
error: `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
19-
--> $DIR/capture-parent-arg.rs:33:6
19+
--> $DIR/capture-parent-arg.rs:34:18
2020
|
2121
LL | impl<'a> W<'a> {
22-
| ^^
23-
LL |
22+
| -- this lifetime parameter is captured
2423
LL | fn bad2() -> impl use<> Into<<Self as Tr>::Assoc> {}
25-
| ------------------------------------ lifetime captured due to being mentioned in the bounds of the `impl Trait`
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime captured due to being mentioned in the bounds of the `impl Trait`
2625

2726
error: aborting due to 2 previous errors; 1 warning emitted
2827

tests/ui/impl-trait/precise-capturing/forgot-to-capture-const.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ LL | #![feature(precise_capturing)]
88
= note: `#[warn(incomplete_features)]` on by default
99

1010
error: `impl Trait` must mention all const parameters in scope
11-
--> $DIR/forgot-to-capture-const.rs:4:13
11+
--> $DIR/forgot-to-capture-const.rs:4:34
1212
|
1313
LL | fn constant<const C: usize>() -> impl use<> Sized {}
14-
| ^^^^^^^^^^^^^^ ---------------- const parameter is implicitly captured by this `impl Trait`
14+
| -------------- ^^^^^^^^^^^^^^^^
15+
| |
16+
| const parameter is implicitly captured by this `impl Trait`
1517
|
1618
= note: currently, all const parameters are required to be mentioned in the precise captures list
1719

tests/ui/impl-trait/precise-capturing/forgot-to-capture-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ fn type_param<T>() -> impl use<> Sized {}
55
//~^ ERROR `impl Trait` must mention all type parameters in scope
66

77
trait Foo {
8-
//~^ ERROR `impl Trait` must mention all type parameters in scope
98
fn bar() -> impl use<> Sized;
9+
//~^ ERROR `impl Trait` must mention all type parameters in scope
1010
}
1111

1212
fn main() {}

tests/ui/impl-trait/precise-capturing/forgot-to-capture-type.stderr

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ LL | #![feature(precise_capturing)]
88
= note: `#[warn(incomplete_features)]` on by default
99

1010
error: `impl Trait` must mention all type parameters in scope
11-
--> $DIR/forgot-to-capture-type.rs:4:15
11+
--> $DIR/forgot-to-capture-type.rs:4:23
1212
|
1313
LL | fn type_param<T>() -> impl use<> Sized {}
14-
| ^ ---------------- type parameter is implicitly captured by this `impl Trait`
14+
| - ^^^^^^^^^^^^^^^^
15+
| |
16+
| type parameter is implicitly captured by this `impl Trait`
1517
|
1618
= note: currently, all type parameters are required to be mentioned in the precise captures list
1719

1820
error: `impl Trait` must mention all type parameters in scope
19-
--> $DIR/forgot-to-capture-type.rs:7:1
21+
--> $DIR/forgot-to-capture-type.rs:8:17
2022
|
2123
LL | trait Foo {
22-
| ^^^^^^^^^
23-
LL |
24+
| --------- type parameter is implicitly captured by this `impl Trait`
2425
LL | fn bar() -> impl use<> Sized;
25-
| ---------------- type parameter is implicitly captured by this `impl Trait`
26+
| ^^^^^^^^^^^^^^^^
2627
|
2728
= note: currently, all type parameters are required to be mentioned in the precise captures list
2829

0 commit comments

Comments
 (0)