@@ -3,11 +3,11 @@ use ast::ptr::P;
3
3
use rustc_ast:: mut_visit:: MutVisitor ;
4
4
use rustc_ast:: visit:: BoundKind ;
5
5
use rustc_ast:: {
6
- self as ast, GenericArg , GenericBound , GenericParamKind , ItemKind , MetaItem ,
6
+ self as ast, GenericArg , GenericBound , GenericParamKind , Generics , ItemKind , MetaItem ,
7
7
TraitBoundModifiers , VariantData , WherePredicate ,
8
8
} ;
9
- use rustc_attr_parsing as attr;
10
9
use rustc_data_structures:: flat_map_in_place:: FlatMapInPlace ;
10
+ use rustc_errors:: E0802 ;
11
11
use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
12
12
use rustc_macros:: Diagnostic ;
13
13
use rustc_span:: { Ident , Span , Symbol , sym} ;
@@ -32,15 +32,6 @@ pub(crate) fn expand_deriving_coerce_pointee(
32
32
let ( name_ident, generics) = if let Annotatable :: Item ( aitem) = item
33
33
&& let ItemKind :: Struct ( struct_data, g) = & aitem. kind
34
34
{
35
- let is_transparent = aitem. attrs . iter ( ) . any ( |attr| {
36
- attr:: find_repr_attrs ( cx. sess , attr)
37
- . into_iter ( )
38
- . any ( |r| matches ! ( r, attr:: ReprTransparent ) )
39
- } ) ;
40
- if !is_transparent {
41
- cx. dcx ( ) . emit_err ( RequireTransparent { span } ) ;
42
- return ;
43
- }
44
35
if !matches ! (
45
36
struct_data,
46
37
VariantData :: Struct { fields, recovered: _ } | VariantData :: Tuple ( fields, _)
@@ -88,8 +79,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
88
79
} else {
89
80
let mut pointees = type_params
90
81
. iter ( )
91
- . filter_map ( |& ( idx, span, is_pointee) | is_pointee. then_some ( ( idx, span) ) )
92
- . fuse ( ) ;
82
+ . filter_map ( |& ( idx, span, is_pointee) | is_pointee. then_some ( ( idx, span) ) ) ;
93
83
match ( pointees. next ( ) , pointees. next ( ) ) {
94
84
( Some ( ( idx, _span) ) , None ) => idx,
95
85
( None , _) => {
@@ -110,6 +100,52 @@ pub(crate) fn expand_deriving_coerce_pointee(
110
100
// Declare helper function that adds implementation blocks.
111
101
// FIXME(dingxiangfei2009): Investigate the set of attributes on target struct to be propagated to impls
112
102
let attrs = thin_vec ! [ cx. attr_word( sym:: automatically_derived, span) , ] ;
103
+ // # Validity assertion which will be checked later in `rustc_hir_analysis::coherence::builtins`.
104
+ {
105
+ let trait_path =
106
+ cx. path_all ( span, true , path ! ( span, core:: marker:: CoercePointeeValidated ) , vec ! [ ] ) ;
107
+ let trait_ref = cx. trait_ref ( trait_path) ;
108
+ push ( Annotatable :: Item (
109
+ cx. item (
110
+ span,
111
+ Ident :: empty ( ) ,
112
+ attrs. clone ( ) ,
113
+ ast:: ItemKind :: Impl ( Box :: new ( ast:: Impl {
114
+ safety : ast:: Safety :: Default ,
115
+ polarity : ast:: ImplPolarity :: Positive ,
116
+ defaultness : ast:: Defaultness :: Final ,
117
+ constness : ast:: Const :: No ,
118
+ generics : Generics {
119
+ params : generics
120
+ . params
121
+ . iter ( )
122
+ . map ( |p| match & p. kind {
123
+ GenericParamKind :: Lifetime => {
124
+ cx. lifetime_param ( p. span ( ) , p. ident , p. bounds . clone ( ) )
125
+ }
126
+ GenericParamKind :: Type { default : _ } => {
127
+ cx. typaram ( p. span ( ) , p. ident , p. bounds . clone ( ) , None )
128
+ }
129
+ GenericParamKind :: Const { ty, kw_span : _, default : _ } => cx
130
+ . const_param (
131
+ p. span ( ) ,
132
+ p. ident ,
133
+ p. bounds . clone ( ) ,
134
+ ty. clone ( ) ,
135
+ None ,
136
+ ) ,
137
+ } )
138
+ . collect ( ) ,
139
+ where_clause : generics. where_clause . clone ( ) ,
140
+ span : generics. span ,
141
+ } ,
142
+ of_trait : Some ( trait_ref) ,
143
+ self_ty : self_type. clone ( ) ,
144
+ items : ThinVec :: new ( ) ,
145
+ } ) ) ,
146
+ ) ,
147
+ ) ) ;
148
+ }
113
149
let mut add_impl_block = |generics, trait_symbol, trait_args| {
114
150
let mut parts = path ! ( span, core:: ops) ;
115
151
parts. push ( Ident :: new ( trait_symbol, span) ) ;
@@ -430,35 +466,35 @@ impl<'a, 'b> rustc_ast::visit::Visitor<'a> for AlwaysErrorOnGenericParam<'a, 'b>
430
466
}
431
467
432
468
#[ derive( Diagnostic ) ]
433
- #[ diag( builtin_macros_coerce_pointee_requires_transparent) ]
469
+ #[ diag( builtin_macros_coerce_pointee_requires_transparent, code = E0802 ) ]
434
470
struct RequireTransparent {
435
471
#[ primary_span]
436
472
span : Span ,
437
473
}
438
474
439
475
#[ derive( Diagnostic ) ]
440
- #[ diag( builtin_macros_coerce_pointee_requires_one_field) ]
476
+ #[ diag( builtin_macros_coerce_pointee_requires_one_field, code = E0802 ) ]
441
477
struct RequireOneField {
442
478
#[ primary_span]
443
479
span : Span ,
444
480
}
445
481
446
482
#[ derive( Diagnostic ) ]
447
- #[ diag( builtin_macros_coerce_pointee_requires_one_generic) ]
483
+ #[ diag( builtin_macros_coerce_pointee_requires_one_generic, code = E0802 ) ]
448
484
struct RequireOneGeneric {
449
485
#[ primary_span]
450
486
span : Span ,
451
487
}
452
488
453
489
#[ derive( Diagnostic ) ]
454
- #[ diag( builtin_macros_coerce_pointee_requires_one_pointee) ]
490
+ #[ diag( builtin_macros_coerce_pointee_requires_one_pointee, code = E0802 ) ]
455
491
struct RequireOnePointee {
456
492
#[ primary_span]
457
493
span : Span ,
458
494
}
459
495
460
496
#[ derive( Diagnostic ) ]
461
- #[ diag( builtin_macros_coerce_pointee_too_many_pointees) ]
497
+ #[ diag( builtin_macros_coerce_pointee_too_many_pointees, code = E0802 ) ]
462
498
struct TooManyPointees {
463
499
#[ primary_span]
464
500
one : Span ,
@@ -467,7 +503,7 @@ struct TooManyPointees {
467
503
}
468
504
469
505
#[ derive( Diagnostic ) ]
470
- #[ diag( builtin_macros_coerce_pointee_requires_maybe_sized) ]
506
+ #[ diag( builtin_macros_coerce_pointee_requires_maybe_sized, code = E0802 ) ]
471
507
struct RequiresMaybeSized {
472
508
#[ primary_span]
473
509
span : Span ,
0 commit comments