]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/check_packed_ref.rs
Rollup merge of #107731 - RalfJung:interpret-discriminant, r=cjgillot
[rust.git] / compiler / rustc_mir_transform / src / check_packed_ref.rs
1 use rustc_errors::struct_span_err;
2 use rustc_middle::mir::visit::{PlaceContext, Visitor};
3 use rustc_middle::mir::*;
4 use rustc_middle::ty::{self, TyCtxt};
5
6 use crate::util;
7 use crate::MirLint;
8
9 pub struct CheckPackedRef;
10
11 impl<'tcx> MirLint<'tcx> for CheckPackedRef {
12     fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
13         let param_env = tcx.param_env(body.source.def_id());
14         let source_info = SourceInfo::outermost(body.span);
15         let mut checker = PackedRefChecker { body, tcx, param_env, source_info };
16         checker.visit_body(&body);
17     }
18 }
19
20 struct PackedRefChecker<'a, 'tcx> {
21     body: &'a Body<'tcx>,
22     tcx: TyCtxt<'tcx>,
23     param_env: ty::ParamEnv<'tcx>,
24     source_info: SourceInfo,
25 }
26
27 impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
28     fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
29         // Make sure we know where in the MIR we are.
30         self.source_info = terminator.source_info;
31         self.super_terminator(terminator, location);
32     }
33
34     fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
35         // Make sure we know where in the MIR we are.
36         self.source_info = statement.source_info;
37         self.super_statement(statement, location);
38     }
39
40     fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
41         if context.is_borrow() {
42             if util::is_disaligned(self.tcx, self.body, self.param_env, *place) {
43                 let def_id = self.body.source.instance.def_id();
44                 if let Some(impl_def_id) = self.tcx.impl_of_method(def_id)
45                     && self.tcx.is_builtin_derive(impl_def_id)
46                 {
47                     // If we ever reach here it means that the generated derive
48                     // code is somehow doing an unaligned reference, which it
49                     // shouldn't do.
50                     unreachable!();
51                 } else {
52                     struct_span_err!(
53                         self.tcx.sess,
54                         self.source_info.span,
55                         E0793,
56                         "reference to packed field is unaligned"
57                     )
58                     .note(
59                         "fields of packed structs are not properly aligned, and creating \
60                         a misaligned reference is undefined behavior (even if that \
61                         reference is never dereferenced)",
62                     ).help(
63                         "copy the field contents to a local variable, or replace the \
64                         reference with a raw pointer and use `read_unaligned`/`write_unaligned` \
65                         (loads and stores via `*p` must be properly aligned even when using raw pointers)"
66                     )
67                     .emit();
68                 }
69             }
70         }
71     }
72 }