Skip to content

Commit

Permalink
Explicitly mention Self
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Apr 20, 2024
1 parent fa0428c commit 57085a0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ hir_analysis_param_in_ty_of_assoc_const_binding =
*[normal] the {$param_def_kind} `{$param_name}` is defined here
}
hir_analysis_param_not_captured = `impl Trait` must mention all {$kind} parameters in scope
hir_analysis_param_not_captured = `impl Trait` must mention all {$kind} parameters in scope in `use<...>`
.label = {$kind} parameter is implicitly captured by this `impl Trait`
.note = currently, all {$kind} parameters are required to be mentioned in the precise captures list
Expand Down Expand Up @@ -405,6 +405,10 @@ hir_analysis_self_in_impl_self =
`Self` is not valid in the self type of an impl block
.note = replace `Self` with a different type
hir_analysis_self_ty_not_captured = `impl Trait` must mention the `Self` type of the trait in `use<...>`
.label = `Self` type parameter is implicitly captured by this `impl Trait`
.note = currently, all type parameters are required to be mentioned in the precise captures list
hir_analysis_simd_ffi_highly_experimental = use of SIMD type{$snip} in FFI is highly experimental and may result in invalid code
.help = add `#![feature(simd_ffi)]` to the crate attributes to enable
Expand Down
20 changes: 14 additions & 6 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,12 +612,20 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
}
}
ty::GenericParamDefKind::Type { .. } => {
// FIXME(precise_capturing): Structured suggestion for this would be useful
tcx.dcx().emit_err(errors::ParamNotCaptured {
param_span: tcx.def_span(param.def_id),
opaque_span: tcx.def_span(opaque_def_id),
kind: "type",
});
if matches!(tcx.def_kind(param.def_id), DefKind::Trait | DefKind::TraitAlias) {
// FIXME(precise_capturing): Structured suggestion for this would be useful
tcx.dcx().emit_err(errors::SelfTyNotCaptured {
trait_span: tcx.def_span(param.def_id),
opaque_span: tcx.def_span(opaque_def_id),
});
} else {
// FIXME(precise_capturing): Structured suggestion for this would be useful
tcx.dcx().emit_err(errors::ParamNotCaptured {
param_span: tcx.def_span(param.def_id),
opaque_span: tcx.def_span(opaque_def_id),
kind: "type",
});
}
}
ty::GenericParamDefKind::Const { .. } => {
// FIXME(precise_capturing): Structured suggestion for this would be useful
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_hir_analysis/src/errors/precise_captures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ pub struct ParamNotCaptured {
pub kind: &'static str,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_self_ty_not_captured)]
#[note]
pub struct SelfTyNotCaptured {
#[primary_span]
pub opaque_span: Span,
#[label]
pub trait_span: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_lifetime_not_captured)]
pub struct LifetimeNotCaptured {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | #![feature(precise_capturing)]
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
= note: `#[warn(incomplete_features)]` on by default

error: `impl Trait` must mention all const parameters in scope
error: `impl Trait` must mention all const parameters in scope in `use<...>`
--> $DIR/forgot-to-capture-const.rs:4:34
|
LL | fn constant<const C: usize>() -> impl use<> Sized {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn type_param<T>() -> impl use<> Sized {}

trait Foo {
fn bar() -> impl use<> Sized;
//~^ ERROR `impl Trait` must mention all type parameters in scope
//~^ ERROR `impl Trait` must mention the `Self` type of the trait
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | #![feature(precise_capturing)]
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
= note: `#[warn(incomplete_features)]` on by default

error: `impl Trait` must mention all type parameters in scope
error: `impl Trait` must mention all type parameters in scope in `use<...>`
--> $DIR/forgot-to-capture-type.rs:4:23
|
LL | fn type_param<T>() -> impl use<> Sized {}
Expand All @@ -17,11 +17,11 @@ LL | fn type_param<T>() -> impl use<> Sized {}
|
= note: currently, all type parameters are required to be mentioned in the precise captures list

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

0 comments on commit 57085a0

Please sign in to comment.