Skip to content

Commit ea631dc

Browse files
authored
Unrolled build for rust-lang#124198
Rollup merge of rust-lang#124198 - compiler-errors:improve-ty-ct-param-span, r=Nadrieril Flip spans for precise capturing syntax not capturing a ty/const param, and for implicit captures of lifetime params Make the primary span point to the opaque, rather than the param which might be very far away (e.g. in an impl header hundreds of lines above).
2 parents fecb7b4 + 57085a0 commit ea631dc

File tree

8 files changed

+69
-38
lines changed

8 files changed

+69
-38
lines changed

compiler/rustc_hir_analysis/messages.ftl

+5-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ hir_analysis_param_in_ty_of_assoc_const_binding =
351351
*[normal] the {$param_def_kind} `{$param_name}` is defined here
352352
}
353353
354-
hir_analysis_param_not_captured = `impl Trait` must mention all {$kind} parameters in scope
354+
hir_analysis_param_not_captured = `impl Trait` must mention all {$kind} parameters in scope in `use<...>`
355355
.label = {$kind} parameter is implicitly captured by this `impl Trait`
356356
.note = currently, all {$kind} parameters are required to be mentioned in the precise captures list
357357
@@ -405,6 +405,10 @@ hir_analysis_self_in_impl_self =
405405
`Self` is not valid in the self type of an impl block
406406
.note = replace `Self` with a different type
407407
408+
hir_analysis_self_ty_not_captured = `impl Trait` must mention the `Self` type of the trait in `use<...>`
409+
.label = `Self` type parameter is implicitly captured by this `impl Trait`
410+
.note = currently, all type parameters are required to be mentioned in the precise captures list
411+
408412
hir_analysis_simd_ffi_highly_experimental = use of SIMD type{$snip} in FFI is highly experimental and may result in invalid code
409413
.help = add `#![feature(simd_ffi)]` to the crate attributes to enable
410414

compiler/rustc_hir_analysis/src/check/check.rs

+32-17
Original file line numberDiff line numberDiff line change
@@ -580,37 +580,52 @@ 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
}
607614
ty::GenericParamDefKind::Type { .. } => {
608-
// FIXME(precise_capturing): Structured suggestion for this would be useful
609-
tcx.dcx().emit_err(errors::ParamNotCaptured {
610-
param_span: tcx.def_span(param.def_id),
611-
opaque_span: tcx.def_span(opaque_def_id),
612-
kind: "type",
613-
});
615+
if matches!(tcx.def_kind(param.def_id), DefKind::Trait | DefKind::TraitAlias) {
616+
// FIXME(precise_capturing): Structured suggestion for this would be useful
617+
tcx.dcx().emit_err(errors::SelfTyNotCaptured {
618+
trait_span: tcx.def_span(param.def_id),
619+
opaque_span: tcx.def_span(opaque_def_id),
620+
});
621+
} else {
622+
// FIXME(precise_capturing): Structured suggestion for this would be useful
623+
tcx.dcx().emit_err(errors::ParamNotCaptured {
624+
param_span: tcx.def_span(param.def_id),
625+
opaque_span: tcx.def_span(opaque_def_id),
626+
kind: "type",
627+
});
628+
}
614629
}
615630
ty::GenericParamDefKind::Const { .. } => {
616631
// FIXME(precise_capturing): Structured suggestion for this would be useful

compiler/rustc_hir_analysis/src/errors/precise_captures.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@ 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

15+
#[derive(Diagnostic)]
16+
#[diag(hir_analysis_self_ty_not_captured)]
17+
#[note]
18+
pub struct SelfTyNotCaptured {
19+
#[primary_span]
20+
pub opaque_span: Span,
21+
#[label]
22+
pub trait_span: Span,
23+
}
24+
1525
#[derive(Diagnostic)]
1626
#[diag(hir_analysis_lifetime_not_captured)]
1727
pub struct LifetimeNotCaptured {
1828
#[primary_span]
1929
pub use_span: Span,
2030
#[label(hir_analysis_param_label)]
21-
pub param_span: Option<Span>,
31+
pub param_span: Span,
2232
#[label]
2333
pub opaque_span: Span,
2434
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ LL | #![feature(precise_capturing)]
77
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
88
= note: `#[warn(incomplete_features)]` on by default
99

10-
error: `impl Trait` must mention all const parameters in scope
11-
--> $DIR/forgot-to-capture-const.rs:4:13
10+
error: `impl Trait` must mention all const parameters in scope in `use<...>`
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 the `Self` type of the trait
1010
}
1111

1212
fn main() {}

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ LL | #![feature(precise_capturing)]
77
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
88
= note: `#[warn(incomplete_features)]` on by default
99

10-
error: `impl Trait` must mention all type parameters in scope
11-
--> $DIR/forgot-to-capture-type.rs:4:15
10+
error: `impl Trait` must mention all type parameters in scope in `use<...>`
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

18-
error: `impl Trait` must mention all type parameters in scope
19-
--> $DIR/forgot-to-capture-type.rs:7:1
20+
error: `impl Trait` must mention the `Self` type of the trait in `use<...>`
21+
--> $DIR/forgot-to-capture-type.rs:8:17
2022
|
2123
LL | trait Foo {
22-
| ^^^^^^^^^
23-
LL |
24+
| --------- `Self` 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)