]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/check_packed_ref.rs
Rollup merge of #98811 - RalfJung:interpret-alloc-range, r=oli-obk
[rust.git] / compiler / rustc_mir_transform / src / check_packed_ref.rs
1 use rustc_hir::def_id::LocalDefId;
2 use rustc_middle::mir::visit::{PlaceContext, Visitor};
3 use rustc_middle::mir::*;
4 use rustc_middle::ty::query::Providers;
5 use rustc_middle::ty::{self, TyCtxt};
6 use rustc_session::lint::builtin::UNALIGNED_REFERENCES;
7
8 use crate::util;
9 use crate::MirLint;
10
11 pub(crate) fn provide(providers: &mut Providers) {
12     *providers = Providers { unsafe_derive_on_repr_packed, ..*providers };
13 }
14
15 pub struct CheckPackedRef;
16
17 impl<'tcx> MirLint<'tcx> for CheckPackedRef {
18     fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
19         let param_env = tcx.param_env(body.source.def_id());
20         let source_info = SourceInfo::outermost(body.span);
21         let mut checker = PackedRefChecker { body, tcx, param_env, source_info };
22         checker.visit_body(&body);
23     }
24 }
25
26 struct PackedRefChecker<'a, 'tcx> {
27     body: &'a Body<'tcx>,
28     tcx: TyCtxt<'tcx>,
29     param_env: ty::ParamEnv<'tcx>,
30     source_info: SourceInfo,
31 }
32
33 fn 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     tcx.struct_span_lint_hir(UNALIGNED_REFERENCES, lint_hir_id, tcx.def_span(def_id), |lint| {
37         // FIXME: when we make this a hard error, this should have its
38         // own error code.
39         let message = if tcx.generics_of(def_id).own_requires_monomorphization() {
40             "`#[derive]` can't be used on a `#[repr(packed)]` struct with \
41              type or const parameters (error E0133)"
42                 .to_string()
43         } else {
44             "`#[derive]` can't be used on a `#[repr(packed)]` struct that \
45              does not derive Copy (error E0133)"
46                 .to_string()
47         };
48         lint.build(&message).emit();
49     });
50 }
51
52 impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
53     fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
54         // Make sure we know where in the MIR we are.
55         self.source_info = terminator.source_info;
56         self.super_terminator(terminator, location);
57     }
58
59     fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
60         // Make sure we know where in the MIR we are.
61         self.source_info = statement.source_info;
62         self.super_statement(statement, location);
63     }
64
65     fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
66         if context.is_borrow() {
67             if util::is_disaligned(self.tcx, self.body, self.param_env, *place) {
68                 let def_id = self.body.source.instance.def_id();
69                 if let Some(impl_def_id) = self
70                     .tcx
71                     .impl_of_method(def_id)
72                     .filter(|&def_id| self.tcx.is_builtin_derive(def_id))
73                 {
74                     // If a method is defined in the local crate,
75                     // the impl containing that method should also be.
76                     self.tcx.ensure().unsafe_derive_on_repr_packed(impl_def_id.expect_local());
77                 } else {
78                     let source_info = self.source_info;
79                     let lint_root = self.body.source_scopes[source_info.scope]
80                         .local_data
81                         .as_ref()
82                         .assert_crate_local()
83                         .lint_root;
84                     self.tcx.struct_span_lint_hir(
85                         UNALIGNED_REFERENCES,
86                         lint_root,
87                         source_info.span,
88                         |lint| {
89                             lint.build("reference to packed field is unaligned")
90                                 .note(
91                                     "fields of packed structs are not properly aligned, and creating \
92                                     a misaligned reference is undefined behavior (even if that \
93                                     reference is never dereferenced)",
94                                 )
95                                 .help(
96                                     "copy the field contents to a local variable, or replace the \
97                                     reference with a raw pointer and use `read_unaligned`/`write_unaligned` \
98                                     (loads and stores via `*p` must be properly aligned even when using raw pointers)"
99                                 )
100                                 .emit();
101                         },
102                     );
103                 }
104             }
105         }
106     }
107 }