]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/types.rs
make `String::new()` const
[rust.git] / src / librustc_lint / types.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(non_snake_case)]
12
13 use rustc::hir::map as hir_map;
14 use rustc::ty::subst::Substs;
15 use rustc::ty::{self, AdtKind, ParamEnv, Ty, TyCtxt};
16 use rustc::ty::layout::{self, IntegerExt, LayoutOf};
17 use util::nodemap::FxHashSet;
18 use lint::{LateContext, LintContext, LintArray};
19 use lint::{LintPass, LateLintPass};
20
21 use std::cmp;
22 use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
23
24 use syntax::{ast, attr};
25 use rustc_target::spec::abi::Abi;
26 use syntax_pos::Span;
27 use syntax::codemap;
28
29 use rustc::hir;
30
31 declare_lint! {
32     UNUSED_COMPARISONS,
33     Warn,
34     "comparisons made useless by limits of the types involved"
35 }
36
37 declare_lint! {
38     OVERFLOWING_LITERALS,
39     Warn,
40     "literal out of range for its type"
41 }
42
43 declare_lint! {
44     VARIANT_SIZE_DIFFERENCES,
45     Allow,
46     "detects enums with widely varying variant sizes"
47 }
48
49 #[derive(Copy, Clone)]
50 pub struct TypeLimits {
51     /// Id of the last visited negated expression
52     negated_expr_id: ast::NodeId,
53 }
54
55 impl TypeLimits {
56     pub fn new() -> TypeLimits {
57         TypeLimits { negated_expr_id: ast::DUMMY_NODE_ID }
58     }
59 }
60
61 impl LintPass for TypeLimits {
62     fn get_lints(&self) -> LintArray {
63         lint_array!(UNUSED_COMPARISONS,
64                     OVERFLOWING_LITERALS)
65     }
66 }
67
68 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
69     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx hir::Expr) {
70         match e.node {
71             hir::ExprUnary(hir::UnNeg, ref expr) => {
72                 // propagate negation, if the negation itself isn't negated
73                 if self.negated_expr_id != e.id {
74                     self.negated_expr_id = expr.id;
75                 }
76             }
77             hir::ExprBinary(binop, ref l, ref r) => {
78                 if is_comparison(binop) && !check_limits(cx, binop, &l, &r) {
79                     cx.span_lint(UNUSED_COMPARISONS,
80                                  e.span,
81                                  "comparison is useless due to type limits");
82                 }
83             }
84             hir::ExprLit(ref lit) => {
85                 match cx.tables.node_id_to_type(e.hir_id).sty {
86                     ty::TyInt(t) => {
87                         match lit.node {
88                             ast::LitKind::Int(v, ast::LitIntType::Signed(_)) |
89                             ast::LitKind::Int(v, ast::LitIntType::Unsuffixed) => {
90                                 let int_type = if let ast::IntTy::Isize = t {
91                                     cx.sess().target.isize_ty
92                                 } else {
93                                     t
94                                 };
95                                 let (_, max) = int_ty_range(int_type);
96                                 let max = max as u128;
97                                 let negative = self.negated_expr_id == e.id;
98
99                                 // Detect literal value out of range [min, max] inclusive
100                                 // avoiding use of -min to prevent overflow/panic
101                                 if (negative && v > max + 1) || (!negative && v > max) {
102                                     if let Some(repr_str) = get_bin_hex_repr(cx, lit) {
103                                         report_bin_hex_error(
104                                             cx,
105                                             e,
106                                             ty::TyInt(t),
107                                             repr_str,
108                                             v,
109                                             negative,
110                                         );
111                                         return;
112                                     }
113                                     cx.span_lint(
114                                         OVERFLOWING_LITERALS,
115                                         e.span,
116                                         &format!("literal out of range for {:?}", t),
117                                     );
118                                     return;
119                                 }
120                             }
121                             _ => bug!(),
122                         };
123                     }
124                     ty::TyUint(t) => {
125                         let uint_type = if let ast::UintTy::Usize = t {
126                             cx.sess().target.usize_ty
127                         } else {
128                             t
129                         };
130                         let (min, max) = uint_ty_range(uint_type);
131                         let lit_val: u128 = match lit.node {
132                             // _v is u8, within range by definition
133                             ast::LitKind::Byte(_v) => return,
134                             ast::LitKind::Int(v, _) => v,
135                             _ => bug!(),
136                         };
137                         if lit_val < min || lit_val > max {
138                             let parent_id = cx.tcx.hir.get_parent_node(e.id);
139                             if let hir_map::NodeExpr(parent_expr) = cx.tcx.hir.get(parent_id) {
140                                 if let hir::ExprCast(..) = parent_expr.node {
141                                     if let ty::TyChar = cx.tables.expr_ty(parent_expr).sty {
142                                         let mut err = cx.struct_span_lint(
143                                                              OVERFLOWING_LITERALS,
144                                                              parent_expr.span,
145                                                              "only u8 can be cast into char");
146                                         err.span_suggestion(parent_expr.span,
147                                                             &"use a char literal instead",
148                                                             format!("'\\u{{{:X}}}'", lit_val));
149                                         err.emit();
150                                         return
151                                     }
152                                 }
153                             }
154                             if let Some(repr_str) = get_bin_hex_repr(cx, lit) {
155                                 report_bin_hex_error(
156                                     cx,
157                                     e,
158                                     ty::TyUint(t),
159                                     repr_str,
160                                     lit_val,
161                                     false,
162                                 );
163                                 return;
164                             }
165                             cx.span_lint(
166                                 OVERFLOWING_LITERALS,
167                                 e.span,
168                                 &format!("literal out of range for {:?}", t),
169                             );
170                         }
171                     }
172                     ty::TyFloat(t) => {
173                         let is_infinite = match lit.node {
174                             ast::LitKind::Float(v, _) |
175                             ast::LitKind::FloatUnsuffixed(v) => {
176                                 match t {
177                                     ast::FloatTy::F32 => v.as_str().parse().map(f32::is_infinite),
178                                     ast::FloatTy::F64 => v.as_str().parse().map(f64::is_infinite),
179                                 }
180                             }
181                             _ => bug!(),
182                         };
183                         if is_infinite == Ok(true) {
184                             cx.span_lint(OVERFLOWING_LITERALS,
185                                          e.span,
186                                          &format!("literal out of range for {:?}", t));
187                         }
188                     }
189                     _ => (),
190                 };
191             }
192             _ => (),
193         };
194
195         fn is_valid<T: cmp::PartialOrd>(binop: hir::BinOp, v: T, min: T, max: T) -> bool {
196             match binop.node {
197                 hir::BiLt => v > min && v <= max,
198                 hir::BiLe => v >= min && v < max,
199                 hir::BiGt => v >= min && v < max,
200                 hir::BiGe => v > min && v <= max,
201                 hir::BiEq | hir::BiNe => v >= min && v <= max,
202                 _ => bug!(),
203             }
204         }
205
206         fn rev_binop(binop: hir::BinOp) -> hir::BinOp {
207             codemap::respan(binop.span,
208                             match binop.node {
209                                 hir::BiLt => hir::BiGt,
210                                 hir::BiLe => hir::BiGe,
211                                 hir::BiGt => hir::BiLt,
212                                 hir::BiGe => hir::BiLe,
213                                 _ => return binop,
214                             })
215         }
216
217         // for isize & usize, be conservative with the warnings, so that the
218         // warnings are consistent between 32- and 64-bit platforms
219         fn int_ty_range(int_ty: ast::IntTy) -> (i128, i128) {
220             match int_ty {
221                 ast::IntTy::Isize => (i64::min_value() as i128, i64::max_value() as i128),
222                 ast::IntTy::I8 => (i8::min_value() as i64 as i128, i8::max_value() as i128),
223                 ast::IntTy::I16 => (i16::min_value() as i64 as i128, i16::max_value() as i128),
224                 ast::IntTy::I32 => (i32::min_value() as i64 as i128, i32::max_value() as i128),
225                 ast::IntTy::I64 => (i64::min_value() as i128, i64::max_value() as i128),
226                 ast::IntTy::I128 =>(i128::min_value() as i128, i128::max_value()),
227             }
228         }
229
230         fn uint_ty_range(uint_ty: ast::UintTy) -> (u128, u128) {
231             match uint_ty {
232                 ast::UintTy::Usize => (u64::min_value() as u128, u64::max_value() as u128),
233                 ast::UintTy::U8 => (u8::min_value() as u128, u8::max_value() as u128),
234                 ast::UintTy::U16 => (u16::min_value() as u128, u16::max_value() as u128),
235                 ast::UintTy::U32 => (u32::min_value() as u128, u32::max_value() as u128),
236                 ast::UintTy::U64 => (u64::min_value() as u128, u64::max_value() as u128),
237                 ast::UintTy::U128 => (u128::min_value(), u128::max_value()),
238             }
239         }
240
241         fn check_limits(cx: &LateContext,
242                         binop: hir::BinOp,
243                         l: &hir::Expr,
244                         r: &hir::Expr)
245                         -> bool {
246             let (lit, expr, swap) = match (&l.node, &r.node) {
247                 (&hir::ExprLit(_), _) => (l, r, true),
248                 (_, &hir::ExprLit(_)) => (r, l, false),
249                 _ => return true,
250             };
251             // Normalize the binop so that the literal is always on the RHS in
252             // the comparison
253             let norm_binop = if swap { rev_binop(binop) } else { binop };
254             match cx.tables.node_id_to_type(expr.hir_id).sty {
255                 ty::TyInt(int_ty) => {
256                     let (min, max) = int_ty_range(int_ty);
257                     let lit_val: i128 = match lit.node {
258                         hir::ExprLit(ref li) => {
259                             match li.node {
260                                 ast::LitKind::Int(v, ast::LitIntType::Signed(_)) |
261                                 ast::LitKind::Int(v, ast::LitIntType::Unsuffixed) => v as i128,
262                                 _ => return true
263                             }
264                         },
265                         _ => bug!()
266                     };
267                     is_valid(norm_binop, lit_val, min, max)
268                 }
269                 ty::TyUint(uint_ty) => {
270                     let (min, max) :(u128, u128) = uint_ty_range(uint_ty);
271                     let lit_val: u128 = match lit.node {
272                         hir::ExprLit(ref li) => {
273                             match li.node {
274                                 ast::LitKind::Int(v, _) => v,
275                                 _ => return true
276                             }
277                         },
278                         _ => bug!()
279                     };
280                     is_valid(norm_binop, lit_val, min, max)
281                 }
282                 _ => true,
283             }
284         }
285
286         fn is_comparison(binop: hir::BinOp) -> bool {
287             match binop.node {
288                 hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe | hir::BiGt => true,
289                 _ => false,
290             }
291         }
292
293         fn get_bin_hex_repr(cx: &LateContext, lit: &ast::Lit) -> Option<String> {
294             let src = cx.sess().codemap().span_to_snippet(lit.span).ok()?;
295             let firstch = src.chars().next()?;
296
297             if firstch == '0' {
298                 match src.chars().nth(1) {
299                     Some('x') | Some('b') => return Some(src),
300                     _ => return None,
301                 }
302             }
303
304             None
305         }
306
307         // This function finds the next fitting type and generates a suggestion string.
308         // It searches for fitting types in the following way (`X < Y`):
309         //  - `iX`: if literal fits in `uX` => `uX`, else => `iY`
310         //  - `-iX` => `iY`
311         //  - `uX` => `uY`
312         //
313         // No suggestion for: `isize`, `usize`.
314         fn get_type_suggestion<'a>(
315             t: &ty::TypeVariants,
316             val: u128,
317             negative: bool,
318         ) -> Option<String> {
319             use syntax::ast::IntTy::*;
320             use syntax::ast::UintTy::*;
321             macro_rules! find_fit {
322                 ($ty:expr, $val:expr, $negative:expr,
323                  $($type:ident => [$($utypes:expr),*] => [$($itypes:expr),*]),+) => {
324                     {
325                         let _neg = if negative { 1 } else { 0 };
326                         match $ty {
327                             $($type => {
328                                 $(if !negative && val <= uint_ty_range($utypes).1 {
329                                     return Some(format!("{:?}", $utypes))
330                                 })*
331                                 $(if val <= int_ty_range($itypes).1 as u128 + _neg {
332                                     return Some(format!("{:?}", $itypes))
333                                 })*
334                                 None
335                             },)*
336                             _ => None
337                         }
338                     }
339                 }
340             }
341             match t {
342                 &ty::TyInt(i) => find_fit!(i, val, negative,
343                               I8 => [U8] => [I16, I32, I64, I128],
344                               I16 => [U16] => [I32, I64, I128],
345                               I32 => [U32] => [I64, I128],
346                               I64 => [U64] => [I128],
347                               I128 => [U128] => []),
348                 &ty::TyUint(u) => find_fit!(u, val, negative,
349                               U8 => [U8, U16, U32, U64, U128] => [],
350                               U16 => [U16, U32, U64, U128] => [],
351                               U32 => [U32, U64, U128] => [],
352                               U64 => [U64, U128] => [],
353                               U128 => [U128] => []),
354                 _ => None,
355             }
356         }
357
358         fn report_bin_hex_error(
359             cx: &LateContext,
360             expr: &hir::Expr,
361             ty: ty::TypeVariants,
362             repr_str: String,
363             val: u128,
364             negative: bool,
365         ) {
366             let (t, actually) = match ty {
367                 ty::TyInt(t) => {
368                     let ity = attr::IntType::SignedInt(t);
369                     let bits = layout::Integer::from_attr(cx.tcx, ity).size().bits();
370                     let actually = (val << (128 - bits)) as i128 >> (128 - bits);
371                     (format!("{:?}", t), actually.to_string())
372                 }
373                 ty::TyUint(t) => {
374                     let ity = attr::IntType::UnsignedInt(t);
375                     let bits = layout::Integer::from_attr(cx.tcx, ity).size().bits();
376                     let actually = (val << (128 - bits)) >> (128 - bits);
377                     (format!("{:?}", t), actually.to_string())
378                 }
379                 _ => bug!(),
380             };
381             let mut err = cx.struct_span_lint(
382                 OVERFLOWING_LITERALS,
383                 expr.span,
384                 &format!("literal out of range for {}", t),
385             );
386             err.note(&format!(
387                 "the literal `{}` (decimal `{}`) does not fit into \
388                  an `{}` and will become `{}{}`",
389                 repr_str, val, t, actually, t
390             ));
391             if let Some(sugg_ty) =
392                 get_type_suggestion(&cx.tables.node_id_to_type(expr.hir_id).sty, val, negative)
393             {
394                 if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') {
395                     let (sans_suffix, _) = repr_str.split_at(pos);
396                     err.span_suggestion(
397                         expr.span,
398                         &format!("consider using `{}` instead", sugg_ty),
399                         format!("{}{}", sans_suffix, sugg_ty),
400                     );
401                 } else {
402                     err.help(&format!("consider using `{}` instead", sugg_ty));
403                 }
404             }
405
406             err.emit();
407         }
408     }
409 }
410
411 declare_lint! {
412     IMPROPER_CTYPES,
413     Warn,
414     "proper use of libc types in foreign modules"
415 }
416
417 struct ImproperCTypesVisitor<'a, 'tcx: 'a> {
418     cx: &'a LateContext<'a, 'tcx>,
419 }
420
421 enum FfiResult<'tcx> {
422     FfiSafe,
423     FfiPhantom(Ty<'tcx>),
424     FfiUnsafe {
425         ty: Ty<'tcx>,
426         reason: &'static str,
427         help: Option<&'static str>,
428     },
429 }
430
431 /// Check if this enum can be safely exported based on the
432 /// "nullable pointer optimization". Currently restricted
433 /// to function pointers and references, but could be
434 /// expanded to cover NonZero raw pointers and newtypes.
435 /// FIXME: This duplicates code in trans.
436 fn is_repr_nullable_ptr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
437                                   def: &'tcx ty::AdtDef,
438                                   substs: &Substs<'tcx>)
439                                   -> bool {
440     if def.variants.len() == 2 {
441         let data_idx;
442
443         if def.variants[0].fields.is_empty() {
444             data_idx = 1;
445         } else if def.variants[1].fields.is_empty() {
446             data_idx = 0;
447         } else {
448             return false;
449         }
450
451         if def.variants[data_idx].fields.len() == 1 {
452             match def.variants[data_idx].fields[0].ty(tcx, substs).sty {
453                 ty::TyFnPtr(_) => {
454                     return true;
455                 }
456                 ty::TyRef(..) => {
457                     return true;
458                 }
459                 _ => {}
460             }
461         }
462     }
463     false
464 }
465
466 impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
467     /// Check if the given type is "ffi-safe" (has a stable, well-defined
468     /// representation which can be exported to C code).
469     fn check_type_for_ffi(&self,
470                           cache: &mut FxHashSet<Ty<'tcx>>,
471                           ty: Ty<'tcx>) -> FfiResult<'tcx> {
472         use self::FfiResult::*;
473
474         let cx = self.cx.tcx;
475
476         // Protect against infinite recursion, for example
477         // `struct S(*mut S);`.
478         // FIXME: A recursion limit is necessary as well, for irregular
479         // recusive types.
480         if !cache.insert(ty) {
481             return FfiSafe;
482         }
483
484         match ty.sty {
485             ty::TyAdt(def, substs) => {
486                 if def.is_phantom_data() {
487                     return FfiPhantom(ty);
488                 }
489                 match def.adt_kind() {
490                     AdtKind::Struct => {
491                         if !def.repr.c() && !def.repr.transparent() {
492                             return FfiUnsafe {
493                                 ty: ty,
494                                 reason: "this struct has unspecified layout",
495                                 help: Some("consider adding a #[repr(C)] or #[repr(transparent)] \
496                                             attribute to this struct"),
497                             };
498                         }
499
500                         if def.non_enum_variant().fields.is_empty() {
501                             return FfiUnsafe {
502                                 ty: ty,
503                                 reason: "this struct has no fields",
504                                 help: Some("consider adding a member to this struct"),
505                             };
506                         }
507
508                         // We can't completely trust repr(C) and repr(transparent) markings;
509                         // make sure the fields are actually safe.
510                         let mut all_phantom = true;
511                         for field in &def.non_enum_variant().fields {
512                             let field_ty = cx.normalize_erasing_regions(
513                                 ParamEnv::reveal_all(),
514                                 field.ty(cx, substs),
515                             );
516                             // repr(transparent) types are allowed to have arbitrary ZSTs, not just
517                             // PhantomData -- skip checking all ZST fields
518                             if def.repr.transparent() {
519                                 let is_zst = cx
520                                     .layout_of(cx.param_env(field.did).and(field_ty))
521                                     .map(|layout| layout.is_zst())
522                                     .unwrap_or(false);
523                                 if is_zst {
524                                     continue;
525                                 }
526                             }
527                             let r = self.check_type_for_ffi(cache, field_ty);
528                             match r {
529                                 FfiSafe => {
530                                     all_phantom = false;
531                                 }
532                                 FfiPhantom(..) => {}
533                                 FfiUnsafe { .. } => {
534                                     return r;
535                                 }
536                             }
537                         }
538
539                         if all_phantom { FfiPhantom(ty) } else { FfiSafe }
540                     }
541                     AdtKind::Union => {
542                         if !def.repr.c() {
543                             return FfiUnsafe {
544                                 ty: ty,
545                                 reason: "this union has unspecified layout",
546                                 help: Some("consider adding a #[repr(C)] attribute to this union"),
547                             };
548                         }
549
550                         if def.non_enum_variant().fields.is_empty() {
551                             return FfiUnsafe {
552                                 ty: ty,
553                                 reason: "this union has no fields",
554                                 help: Some("consider adding a field to this union"),
555                             };
556                         }
557
558                         let mut all_phantom = true;
559                         for field in &def.non_enum_variant().fields {
560                             let field_ty = cx.normalize_erasing_regions(
561                                 ParamEnv::reveal_all(),
562                                 field.ty(cx, substs),
563                             );
564                             let r = self.check_type_for_ffi(cache, field_ty);
565                             match r {
566                                 FfiSafe => {
567                                     all_phantom = false;
568                                 }
569                                 FfiPhantom(..) => {}
570                                 FfiUnsafe { .. } => {
571                                     return r;
572                                 }
573                             }
574                         }
575
576                         if all_phantom { FfiPhantom(ty) } else { FfiSafe }
577                     }
578                     AdtKind::Enum => {
579                         if def.variants.is_empty() {
580                             // Empty enums are okay... although sort of useless.
581                             return FfiSafe;
582                         }
583
584                         // Check for a repr() attribute to specify the size of the
585                         // discriminant.
586                         if !def.repr.c() && def.repr.int.is_none() {
587                             // Special-case types like `Option<extern fn()>`.
588                             if !is_repr_nullable_ptr(cx, def, substs) {
589                                 return FfiUnsafe {
590                                     ty: ty,
591                                     reason: "enum has no representation hint",
592                                     help: Some("consider adding a #[repr(...)] attribute \
593                                                 to this enum"),
594                                 };
595                             }
596                         }
597
598                         // Check the contained variants.
599                         for variant in &def.variants {
600                             for field in &variant.fields {
601                                 let arg = cx.normalize_erasing_regions(
602                                     ParamEnv::reveal_all(),
603                                     field.ty(cx, substs),
604                                 );
605                                 let r = self.check_type_for_ffi(cache, arg);
606                                 match r {
607                                     FfiSafe => {}
608                                     FfiUnsafe { .. } => {
609                                         return r;
610                                     }
611                                     FfiPhantom(..) => {
612                                         return FfiUnsafe {
613                                             ty: ty,
614                                             reason: "this enum contains a PhantomData field",
615                                             help: None,
616                                         };
617                                     }
618                                 }
619                             }
620                         }
621                         FfiSafe
622                     }
623                 }
624             }
625
626             ty::TyChar => FfiUnsafe {
627                 ty: ty,
628                 reason: "the `char` type has no C equivalent",
629                 help: Some("consider using `u32` or `libc::wchar_t` instead"),
630             },
631
632             ty::TyInt(ast::IntTy::I128) | ty::TyUint(ast::UintTy::U128) => FfiUnsafe {
633                 ty: ty,
634                 reason: "128-bit integers don't currently have a known stable ABI",
635                 help: None,
636             },
637
638             // Primitive types with a stable representation.
639             ty::TyBool | ty::TyInt(..) | ty::TyUint(..) | ty::TyFloat(..) | ty::TyNever => FfiSafe,
640
641             ty::TySlice(_) => FfiUnsafe {
642                 ty: ty,
643                 reason: "slices have no C equivalent",
644                 help: Some("consider using a raw pointer instead"),
645             },
646
647             ty::TyDynamic(..) => FfiUnsafe {
648                 ty: ty,
649                 reason: "trait objects have no C equivalent",
650                 help: None,
651             },
652
653             ty::TyStr => FfiUnsafe {
654                 ty: ty,
655                 reason: "string slices have no C equivalent",
656                 help: Some("consider using `*const u8` and a length instead"),
657             },
658
659             ty::TyTuple(..) => FfiUnsafe {
660                 ty: ty,
661                 reason: "tuples have unspecified layout",
662                 help: Some("consider using a struct instead"),
663             },
664
665             ty::TyRawPtr(ref m) |
666             ty::TyRef(_, ref m) => self.check_type_for_ffi(cache, m.ty),
667
668             ty::TyArray(ty, _) => self.check_type_for_ffi(cache, ty),
669
670             ty::TyFnPtr(sig) => {
671                 match sig.abi() {
672                     Abi::Rust | Abi::RustIntrinsic | Abi::PlatformIntrinsic | Abi::RustCall => {
673                         return FfiUnsafe {
674                             ty: ty,
675                             reason: "this function pointer has Rust-specific calling convention",
676                             help: Some("consider using an `fn \"extern\"(...) -> ...` \
677                                         function pointer instead"),
678                         }
679                     }
680                     _ => {}
681                 }
682
683                 let sig = cx.erase_late_bound_regions(&sig);
684                 if !sig.output().is_nil() {
685                     let r = self.check_type_for_ffi(cache, sig.output());
686                     match r {
687                         FfiSafe => {}
688                         _ => {
689                             return r;
690                         }
691                     }
692                 }
693                 for arg in sig.inputs() {
694                     let r = self.check_type_for_ffi(cache, arg);
695                     match r {
696                         FfiSafe => {}
697                         _ => {
698                             return r;
699                         }
700                     }
701                 }
702                 FfiSafe
703             }
704
705             ty::TyForeign(..) => FfiSafe,
706
707             ty::TyParam(..) |
708             ty::TyInfer(..) |
709             ty::TyError |
710             ty::TyClosure(..) |
711             ty::TyGenerator(..) |
712             ty::TyGeneratorWitness(..) |
713             ty::TyProjection(..) |
714             ty::TyAnon(..) |
715             ty::TyFnDef(..) => bug!("Unexpected type in foreign function"),
716         }
717     }
718
719     fn check_type_for_ffi_and_report_errors(&mut self, sp: Span, ty: Ty<'tcx>) {
720         // it is only OK to use this function because extern fns cannot have
721         // any generic types right now:
722         let ty = self.cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
723
724         match self.check_type_for_ffi(&mut FxHashSet(), ty) {
725             FfiResult::FfiSafe => {}
726             FfiResult::FfiPhantom(ty) => {
727                 self.cx.span_lint(IMPROPER_CTYPES,
728                                   sp,
729                                   &format!("`extern` block uses type `{}` which is not FFI-safe: \
730                                             composed only of PhantomData", ty));
731             }
732             FfiResult::FfiUnsafe { ty: unsafe_ty, reason, help } => {
733                 let msg = format!("`extern` block uses type `{}` which is not FFI-safe: {}",
734                                   unsafe_ty, reason);
735                 let mut diag = self.cx.struct_span_lint(IMPROPER_CTYPES, sp, &msg);
736                 if let Some(s) = help {
737                     diag.help(s);
738                 }
739                 if let ty::TyAdt(def, _) = unsafe_ty.sty {
740                     if let Some(sp) = self.cx.tcx.hir.span_if_local(def.did) {
741                         diag.span_note(sp, "type defined here");
742                     }
743                 }
744                 diag.emit();
745             }
746         }
747     }
748
749     fn check_foreign_fn(&mut self, id: ast::NodeId, decl: &hir::FnDecl) {
750         let def_id = self.cx.tcx.hir.local_def_id(id);
751         let sig = self.cx.tcx.fn_sig(def_id);
752         let sig = self.cx.tcx.erase_late_bound_regions(&sig);
753
754         for (input_ty, input_hir) in sig.inputs().iter().zip(&decl.inputs) {
755             self.check_type_for_ffi_and_report_errors(input_hir.span, input_ty);
756         }
757
758         if let hir::Return(ref ret_hir) = decl.output {
759             let ret_ty = sig.output();
760             if !ret_ty.is_nil() {
761                 self.check_type_for_ffi_and_report_errors(ret_hir.span, ret_ty);
762             }
763         }
764     }
765
766     fn check_foreign_static(&mut self, id: ast::NodeId, span: Span) {
767         let def_id = self.cx.tcx.hir.local_def_id(id);
768         let ty = self.cx.tcx.type_of(def_id);
769         self.check_type_for_ffi_and_report_errors(span, ty);
770     }
771 }
772
773 #[derive(Copy, Clone)]
774 pub struct ImproperCTypes;
775
776 impl LintPass for ImproperCTypes {
777     fn get_lints(&self) -> LintArray {
778         lint_array!(IMPROPER_CTYPES)
779     }
780 }
781
782 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImproperCTypes {
783     fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
784         let mut vis = ImproperCTypesVisitor { cx: cx };
785         if let hir::ItemForeignMod(ref nmod) = it.node {
786             if nmod.abi != Abi::RustIntrinsic && nmod.abi != Abi::PlatformIntrinsic {
787                 for ni in &nmod.items {
788                     match ni.node {
789                         hir::ForeignItemFn(ref decl, _, _) => {
790                             vis.check_foreign_fn(ni.id, decl);
791                         }
792                         hir::ForeignItemStatic(ref ty, _) => {
793                             vis.check_foreign_static(ni.id, ty.span);
794                         }
795                         hir::ForeignItemType => ()
796                     }
797                 }
798             }
799         }
800     }
801 }
802
803 pub struct VariantSizeDifferences;
804
805 impl LintPass for VariantSizeDifferences {
806     fn get_lints(&self) -> LintArray {
807         lint_array!(VARIANT_SIZE_DIFFERENCES)
808     }
809 }
810
811 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VariantSizeDifferences {
812     fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
813         if let hir::ItemEnum(ref enum_definition, ref gens) = it.node {
814             if gens.params.iter().all(|param| param.is_lifetime_param()) {
815                 // sizes only make sense for non-generic types
816                 let item_def_id = cx.tcx.hir.local_def_id(it.id);
817                 let t = cx.tcx.type_of(item_def_id);
818                 let ty = cx.tcx.erase_regions(&t);
819                 let layout = cx.layout_of(ty).unwrap_or_else(|e| {
820                     bug!("failed to get layout for `{}`: {}", t, e)
821                 });
822
823                 if let layout::Variants::Tagged { ref variants, ref discr, .. } = layout.variants {
824                     let discr_size = discr.value.size(cx.tcx).bytes();
825
826                     debug!("enum `{}` is {} bytes large with layout:\n{:#?}",
827                       t, layout.size.bytes(), layout);
828
829                     let (largest, slargest, largest_index) = enum_definition.variants
830                         .iter()
831                         .zip(variants)
832                         .map(|(variant, variant_layout)| {
833                             // Subtract the size of the enum discriminant
834                             let bytes = variant_layout.size.bytes()
835                                 .saturating_sub(discr_size);
836
837                             debug!("- variant `{}` is {} bytes large", variant.node.name, bytes);
838                             bytes
839                         })
840                         .enumerate()
841                         .fold((0, 0, 0), |(l, s, li), (idx, size)| if size > l {
842                             (size, l, idx)
843                         } else if size > s {
844                             (l, size, li)
845                         } else {
846                             (l, s, li)
847                         });
848
849                     // we only warn if the largest variant is at least thrice as large as
850                     // the second-largest.
851                     if largest > slargest * 3 && slargest > 0 {
852                         cx.span_lint(VARIANT_SIZE_DIFFERENCES,
853                                      enum_definition.variants[largest_index].span,
854                                      &format!("enum variant is more than three times larger \
855                                                ({} bytes) than the next largest",
856                                               largest));
857                     }
858                 }
859             }
860         }
861     }
862 }