Rollup merge of #133774 - dingxiangfei2009:translatable-coerce-pointee-errors, r=jieyouxu
Make CoercePointee errors translatable Tracked by #123430 Just in case that a translatable error message would become a blocker to stabilization, this PR switches over to fluent error messages, which also slightly improve the wordings and use more accurate span information. cc `@Darksonn` `@traviscross`
This commit is contained in:
commit
4b2d68dc3d
4 changed files with 78 additions and 48 deletions
|
@ -94,6 +94,21 @@ builtin_macros_cfg_accessible_indeterminate = cannot determine whether the path
|
||||||
builtin_macros_cfg_accessible_literal_path = `cfg_accessible` path cannot be a literal
|
builtin_macros_cfg_accessible_literal_path = `cfg_accessible` path cannot be a literal
|
||||||
builtin_macros_cfg_accessible_multiple_paths = multiple `cfg_accessible` paths are specified
|
builtin_macros_cfg_accessible_multiple_paths = multiple `cfg_accessible` paths are specified
|
||||||
builtin_macros_cfg_accessible_unspecified_path = `cfg_accessible` path is not specified
|
builtin_macros_cfg_accessible_unspecified_path = `cfg_accessible` path is not specified
|
||||||
|
|
||||||
|
builtin_macros_coerce_pointee_requires_maybe_sized = `derive(CoercePointee)` requires `{$name}` to be marked `?Sized`
|
||||||
|
|
||||||
|
builtin_macros_coerce_pointee_requires_one_field = `CoercePointee` can only be derived on `struct`s with at least one field
|
||||||
|
|
||||||
|
builtin_macros_coerce_pointee_requires_one_generic = `CoercePointee` can only be derived on `struct`s that are generic over at least one type
|
||||||
|
|
||||||
|
builtin_macros_coerce_pointee_requires_one_pointee = exactly one generic type parameter must be marked as `#[pointee]` to derive `CoercePointee` traits
|
||||||
|
|
||||||
|
builtin_macros_coerce_pointee_requires_transparent = `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
|
||||||
|
|
||||||
|
builtin_macros_coerce_pointee_too_many_pointees = only one type parameter can be marked as `#[pointee]` when deriving `CoercePointee` traits
|
||||||
|
.label = here another type parameter is marked as `#[pointee]`
|
||||||
|
|
||||||
|
|
||||||
builtin_macros_concat_bytes_array = cannot concatenate doubly nested array
|
builtin_macros_concat_bytes_array = cannot concatenate doubly nested array
|
||||||
.note = byte strings are treated as arrays of bytes
|
.note = byte strings are treated as arrays of bytes
|
||||||
.help = try flattening the array
|
.help = try flattening the array
|
||||||
|
|
|
@ -9,6 +9,7 @@ use rustc_ast::{
|
||||||
use rustc_attr as attr;
|
use rustc_attr as attr;
|
||||||
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
|
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
|
||||||
use rustc_expand::base::{Annotatable, ExtCtxt};
|
use rustc_expand::base::{Annotatable, ExtCtxt};
|
||||||
|
use rustc_macros::Diagnostic;
|
||||||
use rustc_span::symbol::{Ident, sym};
|
use rustc_span::symbol::{Ident, sym};
|
||||||
use rustc_span::{Span, Symbol};
|
use rustc_span::{Span, Symbol};
|
||||||
use thin_vec::{ThinVec, thin_vec};
|
use thin_vec::{ThinVec, thin_vec};
|
||||||
|
@ -38,12 +39,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
|
||||||
.any(|r| matches!(r, attr::ReprTransparent))
|
.any(|r| matches!(r, attr::ReprTransparent))
|
||||||
});
|
});
|
||||||
if !is_transparent {
|
if !is_transparent {
|
||||||
cx.dcx()
|
cx.dcx().emit_err(RequireTransparent { span });
|
||||||
.struct_span_err(
|
|
||||||
span,
|
|
||||||
"`CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`",
|
|
||||||
)
|
|
||||||
.emit();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if !matches!(
|
if !matches!(
|
||||||
|
@ -51,22 +47,12 @@ pub(crate) fn expand_deriving_coerce_pointee(
|
||||||
VariantData::Struct { fields, recovered: _ } | VariantData::Tuple(fields, _)
|
VariantData::Struct { fields, recovered: _ } | VariantData::Tuple(fields, _)
|
||||||
if !fields.is_empty())
|
if !fields.is_empty())
|
||||||
{
|
{
|
||||||
cx.dcx()
|
cx.dcx().emit_err(RequireOneField { span });
|
||||||
.struct_span_err(
|
|
||||||
span,
|
|
||||||
"`CoercePointee` can only be derived on `struct`s with at least one field",
|
|
||||||
)
|
|
||||||
.emit();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
(aitem.ident, g)
|
(aitem.ident, g)
|
||||||
} else {
|
} else {
|
||||||
cx.dcx()
|
cx.dcx().emit_err(RequireTransparent { span });
|
||||||
.struct_span_err(
|
|
||||||
span,
|
|
||||||
"`CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`",
|
|
||||||
)
|
|
||||||
.emit();
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -95,10 +81,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
|
||||||
|
|
||||||
let pointee_param_idx = if type_params.is_empty() {
|
let pointee_param_idx = if type_params.is_empty() {
|
||||||
// `#[derive(CoercePointee)]` requires at least one generic type on the target `struct`
|
// `#[derive(CoercePointee)]` requires at least one generic type on the target `struct`
|
||||||
cx.dcx().struct_span_err(
|
cx.dcx().emit_err(RequireOneGeneric { span });
|
||||||
span,
|
|
||||||
"`CoercePointee` can only be derived on `struct`s that are generic over at least one type",
|
|
||||||
).emit();
|
|
||||||
return;
|
return;
|
||||||
} else if type_params.len() == 1 {
|
} else if type_params.len() == 1 {
|
||||||
// Regardless of the only type param being designed as `#[pointee]` or not, we can just use it as such
|
// Regardless of the only type param being designed as `#[pointee]` or not, we can just use it as such
|
||||||
|
@ -111,19 +94,11 @@ pub(crate) fn expand_deriving_coerce_pointee(
|
||||||
match (pointees.next(), pointees.next()) {
|
match (pointees.next(), pointees.next()) {
|
||||||
(Some((idx, _span)), None) => idx,
|
(Some((idx, _span)), None) => idx,
|
||||||
(None, _) => {
|
(None, _) => {
|
||||||
cx.dcx().struct_span_err(
|
cx.dcx().emit_err(RequireOnePointee { span });
|
||||||
span,
|
|
||||||
"exactly one generic type parameter must be marked as #[pointee] to derive CoercePointee traits",
|
|
||||||
).emit();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
(Some((_, one)), Some((_, another))) => {
|
(Some((_, one)), Some((_, another))) => {
|
||||||
cx.dcx()
|
cx.dcx().emit_err(TooManyPointees { one, another });
|
||||||
.struct_span_err(
|
|
||||||
vec![one, another],
|
|
||||||
"only one type parameter can be marked as `#[pointee]` when deriving CoercePointee traits",
|
|
||||||
)
|
|
||||||
.emit();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -181,15 +156,10 @@ pub(crate) fn expand_deriving_coerce_pointee(
|
||||||
pointee_ty_ident.name,
|
pointee_ty_ident.name,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
cx.dcx()
|
cx.dcx().emit_err(RequiresMaybeSized {
|
||||||
.struct_span_err(
|
span: pointee_ty_ident.span,
|
||||||
pointee_ty_ident.span,
|
name: pointee_ty_ident.name.to_ident_string(),
|
||||||
format!(
|
});
|
||||||
"`derive(CoercePointee)` requires {} to be marked `?Sized`",
|
|
||||||
pointee_ty_ident.name
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.emit();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let arg = GenericArg::Type(s_ty.clone());
|
let arg = GenericArg::Type(s_ty.clone());
|
||||||
|
@ -459,3 +429,48 @@ impl<'a, 'b> rustc_ast::visit::Visitor<'a> for AlwaysErrorOnGenericParam<'a, 'b>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(builtin_macros_coerce_pointee_requires_transparent)]
|
||||||
|
struct RequireTransparent {
|
||||||
|
#[primary_span]
|
||||||
|
span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(builtin_macros_coerce_pointee_requires_one_field)]
|
||||||
|
struct RequireOneField {
|
||||||
|
#[primary_span]
|
||||||
|
span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(builtin_macros_coerce_pointee_requires_one_generic)]
|
||||||
|
struct RequireOneGeneric {
|
||||||
|
#[primary_span]
|
||||||
|
span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(builtin_macros_coerce_pointee_requires_one_pointee)]
|
||||||
|
struct RequireOnePointee {
|
||||||
|
#[primary_span]
|
||||||
|
span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(builtin_macros_coerce_pointee_too_many_pointees)]
|
||||||
|
struct TooManyPointees {
|
||||||
|
#[primary_span]
|
||||||
|
one: Span,
|
||||||
|
#[label]
|
||||||
|
another: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(builtin_macros_coerce_pointee_requires_maybe_sized)]
|
||||||
|
struct RequiresMaybeSized {
|
||||||
|
#[primary_span]
|
||||||
|
span: Span,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ struct NoFieldUnit<'a, #[pointee] T: ?Sized>();
|
||||||
struct NoGeneric<'a>(&'a u8);
|
struct NoGeneric<'a>(&'a u8);
|
||||||
|
|
||||||
#[derive(CoercePointee)]
|
#[derive(CoercePointee)]
|
||||||
//~^ ERROR: exactly one generic type parameter must be marked as #[pointee] to derive CoercePointee traits
|
//~^ ERROR: exactly one generic type parameter must be marked as `#[pointee]` to derive `CoercePointee` traits
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
struct AmbiguousPointee<'a, T1: ?Sized, T2: ?Sized> {
|
struct AmbiguousPointee<'a, T1: ?Sized, T2: ?Sized> {
|
||||||
a: (&'a T1, &'a T2),
|
a: (&'a T1, &'a T2),
|
||||||
|
@ -38,7 +38,7 @@ struct AmbiguousPointee<'a, T1: ?Sized, T2: ?Sized> {
|
||||||
#[derive(CoercePointee)]
|
#[derive(CoercePointee)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
struct TooManyPointees<'a, #[pointee] A: ?Sized, #[pointee] B: ?Sized>((&'a A, &'a B));
|
struct TooManyPointees<'a, #[pointee] A: ?Sized, #[pointee] B: ?Sized>((&'a A, &'a B));
|
||||||
//~^ ERROR: only one type parameter can be marked as `#[pointee]` when deriving CoercePointee traits
|
//~^ ERROR: only one type parameter can be marked as `#[pointee]` when deriving `CoercePointee` traits
|
||||||
|
|
||||||
#[derive(CoercePointee)]
|
#[derive(CoercePointee)]
|
||||||
//~^ ERROR: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
|
//~^ ERROR: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
|
||||||
|
@ -49,7 +49,7 @@ struct NotTransparent<'a, #[pointee] T: ?Sized> {
|
||||||
#[derive(CoercePointee)]
|
#[derive(CoercePointee)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
struct NoMaybeSized<'a, #[pointee] T> {
|
struct NoMaybeSized<'a, #[pointee] T> {
|
||||||
//~^ ERROR: `derive(CoercePointee)` requires T to be marked `?Sized`
|
//~^ ERROR: `derive(CoercePointee)` requires `T` to be marked `?Sized`
|
||||||
ptr: &'a T,
|
ptr: &'a T,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ LL | #[derive(CoercePointee)]
|
||||||
|
|
|
|
||||||
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: exactly one generic type parameter must be marked as #[pointee] to derive CoercePointee traits
|
error: exactly one generic type parameter must be marked as `#[pointee]` to derive `CoercePointee` traits
|
||||||
--> $DIR/deriving-coerce-pointee-neg.rs:31:10
|
--> $DIR/deriving-coerce-pointee-neg.rs:31:10
|
||||||
|
|
|
|
||||||
LL | #[derive(CoercePointee)]
|
LL | #[derive(CoercePointee)]
|
||||||
|
@ -38,11 +38,11 @@ LL | #[derive(CoercePointee)]
|
||||||
|
|
|
|
||||||
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: only one type parameter can be marked as `#[pointee]` when deriving CoercePointee traits
|
error: only one type parameter can be marked as `#[pointee]` when deriving `CoercePointee` traits
|
||||||
--> $DIR/deriving-coerce-pointee-neg.rs:40:39
|
--> $DIR/deriving-coerce-pointee-neg.rs:40:39
|
||||||
|
|
|
|
||||||
LL | struct TooManyPointees<'a, #[pointee] A: ?Sized, #[pointee] B: ?Sized>((&'a A, &'a B));
|
LL | struct TooManyPointees<'a, #[pointee] A: ?Sized, #[pointee] B: ?Sized>((&'a A, &'a B));
|
||||||
| ^ ^
|
| ^ - here another type parameter is marked as `#[pointee]`
|
||||||
|
|
||||||
error: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
|
error: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
|
||||||
--> $DIR/deriving-coerce-pointee-neg.rs:43:10
|
--> $DIR/deriving-coerce-pointee-neg.rs:43:10
|
||||||
|
@ -52,7 +52,7 @@ LL | #[derive(CoercePointee)]
|
||||||
|
|
|
|
||||||
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: `derive(CoercePointee)` requires T to be marked `?Sized`
|
error: `derive(CoercePointee)` requires `T` to be marked `?Sized`
|
||||||
--> $DIR/deriving-coerce-pointee-neg.rs:51:36
|
--> $DIR/deriving-coerce-pointee-neg.rs:51:36
|
||||||
|
|
|
|
||||||
LL | struct NoMaybeSized<'a, #[pointee] T> {
|
LL | struct NoMaybeSized<'a, #[pointee] T> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue