Skip to content

Commit 57085a0

Browse files
Explicitly mention Self
1 parent fa0428c commit 57085a0

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
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

+14-6
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,20 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
612612
}
613613
}
614614
ty::GenericParamDefKind::Type { .. } => {
615-
// FIXME(precise_capturing): Structured suggestion for this would be useful
616-
tcx.dcx().emit_err(errors::ParamNotCaptured {
617-
param_span: tcx.def_span(param.def_id),
618-
opaque_span: tcx.def_span(opaque_def_id),
619-
kind: "type",
620-
});
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+
}
621629
}
622630
ty::GenericParamDefKind::Const { .. } => {
623631
// FIXME(precise_capturing): Structured suggestion for this would be useful

compiler/rustc_hir_analysis/src/errors/precise_captures.rs

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ pub struct ParamNotCaptured {
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 {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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
10+
error: `impl Trait` must mention all const parameters in scope in `use<...>`
1111
--> $DIR/forgot-to-capture-const.rs:4:34
1212
|
1313
LL | fn constant<const C: usize>() -> impl use<> Sized {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn type_param<T>() -> impl use<> Sized {}
66

77
trait Foo {
88
fn bar() -> impl use<> Sized;
9-
//~^ ERROR `impl Trait` must mention all type parameters in scope
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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
10+
error: `impl Trait` must mention all type parameters in scope in `use<...>`
1111
--> $DIR/forgot-to-capture-type.rs:4:23
1212
|
1313
LL | fn type_param<T>() -> impl use<> Sized {}
@@ -17,11 +17,11 @@ LL | fn type_param<T>() -> impl use<> Sized {}
1717
|
1818
= note: currently, all type parameters are required to be mentioned in the precise captures list
1919

20-
error: `impl Trait` must mention all type parameters in scope
20+
error: `impl Trait` must mention the `Self` type of the trait in `use<...>`
2121
--> $DIR/forgot-to-capture-type.rs:8:17
2222
|
2323
LL | trait Foo {
24-
| --------- type parameter is implicitly captured by this `impl Trait`
24+
| --------- `Self` type parameter is implicitly captured by this `impl Trait`
2525
LL | fn bar() -> impl use<> Sized;
2626
| ^^^^^^^^^^^^^^^^
2727
|

0 commit comments

Comments
 (0)