1+ use rustc_errors:: struct_span_err;
12use rustc_hir:: def_id:: LocalDefId ;
23use rustc_middle:: mir:: visit:: { PlaceContext , Visitor } ;
34use rustc_middle:: mir:: * ;
45use rustc_middle:: ty:: query:: Providers ;
56use rustc_middle:: ty:: { self , TyCtxt } ;
6- use rustc_session:: lint:: builtin:: UNALIGNED_REFERENCES ;
77
88use crate :: util;
99use crate :: MirLint ;
@@ -31,11 +31,6 @@ struct PackedRefChecker<'a, 'tcx> {
3131}
3232
3333fn unsafe_derive_on_repr_packed ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) {
34- let lint_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
35-
36- // FIXME: when we make this a hard error, this should have its
37- // own error code.
38-
3934 let extra = if tcx. generics_of ( def_id) . own_requires_monomorphization ( ) {
4035 "with type or const parameters"
4136 } else {
@@ -46,14 +41,7 @@ fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
4641 tcx. item_name( tcx. trait_id_of_impl( def_id. to_def_id( ) ) . expect( "derived trait name" ) ) ,
4742 extra
4843 ) ;
49-
50- tcx. struct_span_lint_hir (
51- UNALIGNED_REFERENCES ,
52- lint_hir_id,
53- tcx. def_span ( def_id) ,
54- message,
55- |lint| lint,
56- ) ;
44+ struct_span_err ! ( tcx. sess, tcx. def_span( def_id) , E0791 , "{message}" ) . emit ( ) ;
5745}
5846
5947impl < ' tcx > Visitor < ' tcx > for PackedRefChecker < ' _ , ' tcx > {
@@ -82,31 +70,22 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
8270 // the impl containing that method should also be.
8371 self . tcx . ensure ( ) . unsafe_derive_on_repr_packed ( impl_def_id. expect_local ( ) ) ;
8472 } else {
85- let source_info = self . source_info ;
86- let lint_root = self . body . source_scopes [ source_info. scope ]
87- . local_data
88- . as_ref ( )
89- . assert_crate_local ( )
90- . lint_root ;
91- self . tcx . struct_span_lint_hir (
92- UNALIGNED_REFERENCES ,
93- lint_root,
94- source_info. span ,
95- "reference to packed field is unaligned" ,
96- |lint| {
97- lint
98- . note (
99- "fields of packed structs are not properly aligned, and creating \
100- a misaligned reference is undefined behavior (even if that \
101- reference is never dereferenced)",
102- )
103- . help (
104- "copy the field contents to a local variable, or replace the \
105- reference with a raw pointer and use `read_unaligned`/`write_unaligned` \
106- (loads and stores via `*p` must be properly aligned even when using raw pointers)"
107- )
108- } ,
109- ) ;
73+ struct_span_err ! (
74+ self . tcx. sess,
75+ self . source_info. span,
76+ E0791 ,
77+ "reference to packed field is unaligned"
78+ )
79+ . note (
80+ "fields of packed structs are not properly aligned, and creating \
81+ a misaligned reference is undefined behavior (even if that \
82+ reference is never dereferenced)",
83+ ) . help (
84+ "copy the field contents to a local variable, or replace the \
85+ reference with a raw pointer and use `read_unaligned`/`write_unaligned` \
86+ (loads and stores via `*p` must be properly aligned even when using raw pointers)"
87+ )
88+ . emit ( ) ;
11089 }
11190 }
11291 }
0 commit comments