Skip to content

Commit 12853ff

Browse files
authored
Unrolled build for rust-lang#133774
Rollup merge of rust-lang#133774 - dingxiangfei2009:translatable-coerce-pointee-errors, r=jieyouxu Make CoercePointee errors translatable Tracked by rust-lang#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`
2 parents 96e51d9 + 836ab5c commit 12853ff

File tree

4 files changed

+78
-48
lines changed

4 files changed

+78
-48
lines changed

compiler/rustc_builtin_macros/messages.ftl

+15
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ builtin_macros_cfg_accessible_indeterminate = cannot determine whether the path
9494
builtin_macros_cfg_accessible_literal_path = `cfg_accessible` path cannot be a literal
9595
builtin_macros_cfg_accessible_multiple_paths = multiple `cfg_accessible` paths are specified
9696
builtin_macros_cfg_accessible_unspecified_path = `cfg_accessible` path is not specified
97+
98+
builtin_macros_coerce_pointee_requires_maybe_sized = `derive(CoercePointee)` requires `{$name}` to be marked `?Sized`
99+
100+
builtin_macros_coerce_pointee_requires_one_field = `CoercePointee` can only be derived on `struct`s with at least one field
101+
102+
builtin_macros_coerce_pointee_requires_one_generic = `CoercePointee` can only be derived on `struct`s that are generic over at least one type
103+
104+
builtin_macros_coerce_pointee_requires_one_pointee = exactly one generic type parameter must be marked as `#[pointee]` to derive `CoercePointee` traits
105+
106+
builtin_macros_coerce_pointee_requires_transparent = `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
107+
108+
builtin_macros_coerce_pointee_too_many_pointees = only one type parameter can be marked as `#[pointee]` when deriving `CoercePointee` traits
109+
.label = here another type parameter is marked as `#[pointee]`
110+
111+
97112
builtin_macros_concat_bytes_array = cannot concatenate doubly nested array
98113
.note = byte strings are treated as arrays of bytes
99114
.help = try flattening the array

compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs

+56-41
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_ast::{
99
use rustc_attr as attr;
1010
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1111
use rustc_expand::base::{Annotatable, ExtCtxt};
12+
use rustc_macros::Diagnostic;
1213
use rustc_span::symbol::{Ident, sym};
1314
use rustc_span::{Span, Symbol};
1415
use thin_vec::{ThinVec, thin_vec};
@@ -38,35 +39,20 @@ pub(crate) fn expand_deriving_coerce_pointee(
3839
.any(|r| matches!(r, attr::ReprTransparent))
3940
});
4041
if !is_transparent {
41-
cx.dcx()
42-
.struct_span_err(
43-
span,
44-
"`CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`",
45-
)
46-
.emit();
42+
cx.dcx().emit_err(RequireTransparent { span });
4743
return;
4844
}
4945
if !matches!(
5046
struct_data,
5147
VariantData::Struct { fields, recovered: _ } | VariantData::Tuple(fields, _)
5248
if !fields.is_empty())
5349
{
54-
cx.dcx()
55-
.struct_span_err(
56-
span,
57-
"`CoercePointee` can only be derived on `struct`s with at least one field",
58-
)
59-
.emit();
50+
cx.dcx().emit_err(RequireOneField { span });
6051
return;
6152
}
6253
(aitem.ident, g)
6354
} else {
64-
cx.dcx()
65-
.struct_span_err(
66-
span,
67-
"`CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`",
68-
)
69-
.emit();
55+
cx.dcx().emit_err(RequireTransparent { span });
7056
return;
7157
};
7258

@@ -95,10 +81,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
9581

9682
let pointee_param_idx = if type_params.is_empty() {
9783
// `#[derive(CoercePointee)]` requires at least one generic type on the target `struct`
98-
cx.dcx().struct_span_err(
99-
span,
100-
"`CoercePointee` can only be derived on `struct`s that are generic over at least one type",
101-
).emit();
84+
cx.dcx().emit_err(RequireOneGeneric { span });
10285
return;
10386
} else if type_params.len() == 1 {
10487
// 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(
11194
match (pointees.next(), pointees.next()) {
11295
(Some((idx, _span)), None) => idx,
11396
(None, _) => {
114-
cx.dcx().struct_span_err(
115-
span,
116-
"exactly one generic type parameter must be marked as #[pointee] to derive CoercePointee traits",
117-
).emit();
97+
cx.dcx().emit_err(RequireOnePointee { span });
11898
return;
11999
}
120100
(Some((_, one)), Some((_, another))) => {
121-
cx.dcx()
122-
.struct_span_err(
123-
vec![one, another],
124-
"only one type parameter can be marked as `#[pointee]` when deriving CoercePointee traits",
125-
)
126-
.emit();
101+
cx.dcx().emit_err(TooManyPointees { one, another });
127102
return;
128103
}
129104
}
@@ -181,15 +156,10 @@ pub(crate) fn expand_deriving_coerce_pointee(
181156
pointee_ty_ident.name,
182157
)
183158
{
184-
cx.dcx()
185-
.struct_span_err(
186-
pointee_ty_ident.span,
187-
format!(
188-
"`derive(CoercePointee)` requires {} to be marked `?Sized`",
189-
pointee_ty_ident.name
190-
),
191-
)
192-
.emit();
159+
cx.dcx().emit_err(RequiresMaybeSized {
160+
span: pointee_ty_ident.span,
161+
name: pointee_ty_ident.name.to_ident_string(),
162+
});
193163
return;
194164
}
195165
let arg = GenericArg::Type(s_ty.clone());
@@ -459,3 +429,48 @@ impl<'a, 'b> rustc_ast::visit::Visitor<'a> for AlwaysErrorOnGenericParam<'a, 'b>
459429
}
460430
}
461431
}
432+
433+
#[derive(Diagnostic)]
434+
#[diag(builtin_macros_coerce_pointee_requires_transparent)]
435+
struct RequireTransparent {
436+
#[primary_span]
437+
span: Span,
438+
}
439+
440+
#[derive(Diagnostic)]
441+
#[diag(builtin_macros_coerce_pointee_requires_one_field)]
442+
struct RequireOneField {
443+
#[primary_span]
444+
span: Span,
445+
}
446+
447+
#[derive(Diagnostic)]
448+
#[diag(builtin_macros_coerce_pointee_requires_one_generic)]
449+
struct RequireOneGeneric {
450+
#[primary_span]
451+
span: Span,
452+
}
453+
454+
#[derive(Diagnostic)]
455+
#[diag(builtin_macros_coerce_pointee_requires_one_pointee)]
456+
struct RequireOnePointee {
457+
#[primary_span]
458+
span: Span,
459+
}
460+
461+
#[derive(Diagnostic)]
462+
#[diag(builtin_macros_coerce_pointee_too_many_pointees)]
463+
struct TooManyPointees {
464+
#[primary_span]
465+
one: Span,
466+
#[label]
467+
another: Span,
468+
}
469+
470+
#[derive(Diagnostic)]
471+
#[diag(builtin_macros_coerce_pointee_requires_maybe_sized)]
472+
struct RequiresMaybeSized {
473+
#[primary_span]
474+
span: Span,
475+
name: String,
476+
}

tests/ui/deriving/deriving-coerce-pointee-neg.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct NoFieldUnit<'a, #[pointee] T: ?Sized>();
2929
struct NoGeneric<'a>(&'a u8);
3030

3131
#[derive(CoercePointee)]
32-
//~^ ERROR: exactly one generic type parameter must be marked as #[pointee] to derive CoercePointee traits
32+
//~^ ERROR: exactly one generic type parameter must be marked as `#[pointee]` to derive `CoercePointee` traits
3333
#[repr(transparent)]
3434
struct AmbiguousPointee<'a, T1: ?Sized, T2: ?Sized> {
3535
a: (&'a T1, &'a T2),
@@ -38,7 +38,7 @@ struct AmbiguousPointee<'a, T1: ?Sized, T2: ?Sized> {
3838
#[derive(CoercePointee)]
3939
#[repr(transparent)]
4040
struct TooManyPointees<'a, #[pointee] A: ?Sized, #[pointee] B: ?Sized>((&'a A, &'a B));
41-
//~^ ERROR: only one type parameter can be marked as `#[pointee]` when deriving CoercePointee traits
41+
//~^ ERROR: only one type parameter can be marked as `#[pointee]` when deriving `CoercePointee` traits
4242

4343
#[derive(CoercePointee)]
4444
//~^ ERROR: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
@@ -49,7 +49,7 @@ struct NotTransparent<'a, #[pointee] T: ?Sized> {
4949
#[derive(CoercePointee)]
5050
#[repr(transparent)]
5151
struct NoMaybeSized<'a, #[pointee] T> {
52-
//~^ ERROR: `derive(CoercePointee)` requires T to be marked `?Sized`
52+
//~^ ERROR: `derive(CoercePointee)` requires `T` to be marked `?Sized`
5353
ptr: &'a T,
5454
}
5555

tests/ui/deriving/deriving-coerce-pointee-neg.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ LL | #[derive(CoercePointee)]
3030
|
3131
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
3232

33-
error: exactly one generic type parameter must be marked as #[pointee] to derive CoercePointee traits
33+
error: exactly one generic type parameter must be marked as `#[pointee]` to derive `CoercePointee` traits
3434
--> $DIR/deriving-coerce-pointee-neg.rs:31:10
3535
|
3636
LL | #[derive(CoercePointee)]
3737
| ^^^^^^^^^^^^^
3838
|
3939
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
4040

41-
error: only one type parameter can be marked as `#[pointee]` when deriving CoercePointee traits
41+
error: only one type parameter can be marked as `#[pointee]` when deriving `CoercePointee` traits
4242
--> $DIR/deriving-coerce-pointee-neg.rs:40:39
4343
|
4444
LL | struct TooManyPointees<'a, #[pointee] A: ?Sized, #[pointee] B: ?Sized>((&'a A, &'a B));
45-
| ^ ^
45+
| ^ - here another type parameter is marked as `#[pointee]`
4646

4747
error: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
4848
--> $DIR/deriving-coerce-pointee-neg.rs:43:10
@@ -52,7 +52,7 @@ LL | #[derive(CoercePointee)]
5252
|
5353
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
5454

55-
error: `derive(CoercePointee)` requires T to be marked `?Sized`
55+
error: `derive(CoercePointee)` requires `T` to be marked `?Sized`
5656
--> $DIR/deriving-coerce-pointee-neg.rs:51:36
5757
|
5858
LL | struct NoMaybeSized<'a, #[pointee] T> {

0 commit comments

Comments
 (0)