Skip to content

Commit c322dbb

Browse files
committed
Auto merge of #24333 - arielb1:implement-rfc401, r=nrc
2 parents f34ff7a + e7e1fd2 commit c322dbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1071
-484
lines changed

src/liballoc/arc.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ impl<T: ?Sized> Drop for Arc<T> {
394394
// it's run more than once)
395395
let ptr = *self._ptr;
396396
// if ptr.is_null() { return }
397-
if ptr as usize == 0 || ptr as usize == mem::POST_DROP_USIZE { return }
397+
if ptr as *mut u8 as usize == 0 || ptr as *mut u8 as usize == mem::POST_DROP_USIZE {
398+
return
399+
}
398400

399401
// Because `fetch_sub` is already atomic, we do not need to synchronize
400402
// with other threads unless we are going to delete the object. This
@@ -524,7 +526,9 @@ impl<T: ?Sized> Drop for Weak<T> {
524526
let ptr = *self._ptr;
525527

526528
// see comments above for why this check is here
527-
if ptr as usize == 0 || ptr as usize == mem::POST_DROP_USIZE { return }
529+
if ptr as *mut u8 as usize == 0 || ptr as *mut u8 as usize == mem::POST_DROP_USIZE {
530+
return
531+
}
528532

529533
// If we find out that we were the last weak pointer, then its time to
530534
// deallocate the data entirely. See the discussion in Arc::drop() about

src/liballoc/rc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl<T: ?Sized> Drop for Rc<T> {
543543
unsafe {
544544
let ptr = *self._ptr;
545545
if !(*(&ptr as *const _ as *const *const ())).is_null() &&
546-
ptr as usize != mem::POST_DROP_USIZE {
546+
ptr as *const () as usize != mem::POST_DROP_USIZE {
547547
self.dec_strong();
548548
if self.strong() == 0 {
549549
// destroy the contained object
@@ -1051,7 +1051,7 @@ impl<T: ?Sized> Drop for Weak<T> {
10511051
unsafe {
10521052
let ptr = *self._ptr;
10531053
if !(*(&ptr as *const _ as *const *const ())).is_null() &&
1054-
ptr as usize != mem::POST_DROP_USIZE {
1054+
ptr as *const () as usize != mem::POST_DROP_USIZE {
10551055
self.dec_weak();
10561056
// the weak count starts at 1, and will only go to zero if all
10571057
// the strong pointers have disappeared.

src/librustc/diagnostics.rs

-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,6 @@ struct Foo<T: 'static> {
801801

802802
register_diagnostics! {
803803
E0011,
804-
E0012,
805804
E0014,
806805
E0016,
807806
E0017,

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub mod back {
9494
pub mod middle {
9595
pub mod astconv_util;
9696
pub mod astencode;
97+
pub mod cast;
9798
pub mod cfg;
9899
pub mod check_const;
99100
pub mod check_static_recursion;

src/librustc/metadata/common.rs

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ enum_from_u32! {
148148
tag_table_capture_modes = 0x67,
149149
tag_table_object_cast_map = 0x68,
150150
tag_table_const_qualif = 0x69,
151+
tag_table_cast_kinds = 0x6a,
151152
}
152153
}
153154

src/librustc/middle/astencode.rs

+25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use metadata::tydecode;
2323
use metadata::tydecode::{DefIdSource, NominalType, TypeWithId, TypeParameter};
2424
use metadata::tydecode::{RegionParameter, ClosureSource};
2525
use metadata::tyencode;
26+
use middle::cast;
2627
use middle::check_const::ConstQualif;
2728
use middle::mem_categorization::Typer;
2829
use middle::privacy::{AllPublic, LastMod};
@@ -688,6 +689,10 @@ pub fn encode_closure_kind(ebml_w: &mut Encoder, kind: ty::ClosureKind) {
688689
kind.encode(ebml_w).unwrap();
689690
}
690691

692+
pub fn encode_cast_kind(ebml_w: &mut Encoder, kind: cast::CastKind) {
693+
kind.encode(ebml_w).unwrap();
694+
}
695+
691696
pub trait vtable_decoder_helpers<'tcx> {
692697
fn read_vec_per_param_space<T, F>(&mut self, f: F) -> VecPerParamSpace<T> where
693698
F: FnMut(&mut Self) -> T;
@@ -1248,6 +1253,13 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
12481253
})
12491254
}
12501255

1256+
if let Some(cast_kind) = tcx.cast_kinds.borrow().get(&id) {
1257+
rbml_w.tag(c::tag_table_cast_kinds, |rbml_w| {
1258+
rbml_w.id(id);
1259+
encode_cast_kind(rbml_w, *cast_kind)
1260+
})
1261+
}
1262+
12511263
for &qualif in tcx.const_qualif_map.borrow().get(&id).iter() {
12521264
rbml_w.tag(c::tag_table_const_qualif, |rbml_w| {
12531265
rbml_w.id(id);
@@ -1289,6 +1301,8 @@ trait rbml_decoder_decoder_helpers<'tcx> {
12891301
-> subst::Substs<'tcx>;
12901302
fn read_auto_adjustment<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
12911303
-> ty::AutoAdjustment<'tcx>;
1304+
fn read_cast_kind<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
1305+
-> cast::CastKind;
12921306
fn read_closure_kind<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
12931307
-> ty::ClosureKind;
12941308
fn read_closure_ty<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
@@ -1641,6 +1655,12 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
16411655
}).unwrap()
16421656
}
16431657

1658+
fn read_cast_kind<'b, 'c>(&mut self, _dcx: &DecodeContext<'b, 'c, 'tcx>)
1659+
-> cast::CastKind
1660+
{
1661+
Decodable::decode(self).unwrap()
1662+
}
1663+
16441664
fn read_closure_kind<'b, 'c>(&mut self, _dcx: &DecodeContext<'b, 'c, 'tcx>)
16451665
-> ty::ClosureKind
16461666
{
@@ -1801,6 +1821,11 @@ fn decode_side_tables(dcx: &DecodeContext,
18011821
dcx.tcx.closure_kinds.borrow_mut().insert(ast_util::local_def(id),
18021822
closure_kind);
18031823
}
1824+
c::tag_table_cast_kinds => {
1825+
let cast_kind =
1826+
val_dsr.read_cast_kind(dcx);
1827+
dcx.tcx.cast_kinds.borrow_mut().insert(id, cast_kind);
1828+
}
18041829
c::tag_table_const_qualif => {
18051830
let qualif: ConstQualif = Decodable::decode(val_dsr).unwrap();
18061831
dcx.tcx.const_qualif_map.borrow_mut().insert(id, qualif);

src/librustc/middle/cast.rs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 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+
// Helpers for handling cast expressions, used in both
12+
// typeck and trans.
13+
14+
use middle::ty::{self, Ty};
15+
16+
use syntax::ast;
17+
18+
/// Types that are represented as ints.
19+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
20+
pub enum IntTy {
21+
U(ast::UintTy),
22+
I,
23+
CEnum,
24+
Bool,
25+
Char
26+
}
27+
28+
// Valid types for the result of a non-coercion cast
29+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
30+
pub enum CastTy<'tcx> {
31+
/// Various types that are represented as ints and handled mostly
32+
/// in the same way, merged for easier matching.
33+
Int(IntTy),
34+
/// Floating-Point types
35+
Float,
36+
/// Function Pointers
37+
FnPtr,
38+
/// Raw pointers
39+
Ptr(&'tcx ty::mt<'tcx>),
40+
/// References
41+
RPtr(&'tcx ty::mt<'tcx>),
42+
}
43+
44+
/// Cast Kind. See RFC 401 (or librustc_typeck/check/cast.rs)
45+
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
46+
pub enum CastKind {
47+
CoercionCast,
48+
PtrPtrCast,
49+
PtrAddrCast,
50+
AddrPtrCast,
51+
NumericCast,
52+
EnumCast,
53+
PrimIntCast,
54+
U8CharCast,
55+
ArrayPtrCast,
56+
FnPtrPtrCast,
57+
FnPtrAddrCast
58+
}
59+
60+
impl<'tcx> CastTy<'tcx> {
61+
pub fn from_ty(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>)
62+
-> Option<CastTy<'tcx>> {
63+
match t.sty {
64+
ty::ty_bool => Some(CastTy::Int(IntTy::Bool)),
65+
ty::ty_char => Some(CastTy::Int(IntTy::Char)),
66+
ty::ty_int(_) => Some(CastTy::Int(IntTy::I)),
67+
ty::ty_uint(u) => Some(CastTy::Int(IntTy::U(u))),
68+
ty::ty_float(_) => Some(CastTy::Float),
69+
ty::ty_enum(..) if ty::type_is_c_like_enum(
70+
tcx, t) => Some(CastTy::Int(IntTy::CEnum)),
71+
ty::ty_ptr(ref mt) => Some(CastTy::Ptr(mt)),
72+
ty::ty_rptr(_, ref mt) => Some(CastTy::RPtr(mt)),
73+
ty::ty_bare_fn(..) => Some(CastTy::FnPtr),
74+
_ => None,
75+
}
76+
}
77+
}

src/librustc/middle/check_const.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// - It's not possible to take the address of a static item with unsafe interior. This is enforced
2525
// by borrowck::gather_loans
2626

27+
use middle::cast::{CastKind};
2728
use middle::const_eval;
2829
use middle::def;
2930
use middle::expr_use_visitor as euv;
@@ -32,11 +33,10 @@ use middle::mem_categorization as mc;
3233
use middle::traits;
3334
use middle::ty::{self, Ty};
3435
use util::nodemap::NodeMap;
35-
use util::ppaux;
36+
use util::ppaux::Repr;
3637

3738
use syntax::ast;
3839
use syntax::codemap::Span;
39-
use syntax::print::pprust;
4040
use syntax::visit::{self, Visitor};
4141

4242
use std::collections::hash_map::Entry;
@@ -197,7 +197,7 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
197197

198198
impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
199199
fn visit_item(&mut self, i: &ast::Item) {
200-
debug!("visit_item(item={})", pprust::item_to_string(i));
200+
debug!("visit_item(item={})", i.repr(self.tcx));
201201
match i.node {
202202
ast::ItemStatic(_, ast::MutImmutable, ref expr) => {
203203
self.check_static_type(&**expr);
@@ -440,26 +440,17 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
440440
}
441441
}
442442
ast::ExprCast(ref from, _) => {
443-
let toty = ty::expr_ty(v.tcx, e);
444-
let fromty = ty::expr_ty(v.tcx, &**from);
445-
let is_legal_cast =
446-
ty::type_is_numeric(toty) ||
447-
ty::type_is_unsafe_ptr(toty) ||
448-
(ty::type_is_bare_fn(toty) && ty::type_is_bare_fn_item(fromty));
449-
if !is_legal_cast {
450-
v.add_qualif(ConstQualif::NOT_CONST);
451-
if v.mode != Mode::Var {
452-
span_err!(v.tcx.sess, e.span, E0012,
453-
"can not cast to `{}` in {}s",
454-
ppaux::ty_to_string(v.tcx, toty), v.msg());
455-
}
456-
}
457-
if ty::type_is_unsafe_ptr(fromty) && ty::type_is_numeric(toty) {
458-
v.add_qualif(ConstQualif::NOT_CONST);
459-
if v.mode != Mode::Var {
460-
span_err!(v.tcx.sess, e.span, E0018,
461-
"can not cast a pointer to an integer in {}s", v.msg());
443+
debug!("Checking const cast(id={})", from.id);
444+
match v.tcx.cast_kinds.borrow().get(&from.id) {
445+
None => v.tcx.sess.span_bug(e.span, "no kind for cast"),
446+
Some(&CastKind::PtrAddrCast) | Some(&CastKind::FnPtrAddrCast) => {
447+
v.add_qualif(ConstQualif::NOT_CONST);
448+
if v.mode != Mode::Var {
449+
span_err!(v.tcx.sess, e.span, E0018,
450+
"can't cast a pointer to an integer in {}s", v.msg());
451+
}
462452
}
453+
_ => {}
463454
}
464455
}
465456
ast::ExprPath(..) => {

src/librustc/middle/const_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ fn cast_const<'tcx>(tcx: &ty::ctxt<'tcx>, val: const_val, ty: Ty) -> CastResult
10021002
macro_rules! convert_val {
10031003
($intermediate_ty:ty, $const_type:ident, $target_ty:ty) => {
10041004
match val {
1005-
const_bool(b) => Ok($const_type(b as $intermediate_ty as $target_ty)),
1005+
const_bool(b) => Ok($const_type(b as u64 as $intermediate_ty as $target_ty)),
10061006
const_uint(u) => Ok($const_type(u as $intermediate_ty as $target_ty)),
10071007
const_int(i) => Ok($const_type(i as $intermediate_ty as $target_ty)),
10081008
const_float(f) => Ok($const_type(f as $intermediate_ty as $target_ty)),

src/librustc/middle/ty.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use session::Session;
4141
use lint;
4242
use metadata::csearch;
4343
use middle;
44+
use middle::cast;
4445
use middle::check_const;
4546
use middle::const_eval;
4647
use middle::def::{self, DefMap, ExportMap};
@@ -288,15 +289,6 @@ pub struct field_ty {
288289
pub origin: ast::DefId, // The DefId of the struct in which the field is declared.
289290
}
290291

291-
// Contains information needed to resolve types and (in the future) look up
292-
// the types of AST nodes.
293-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
294-
pub struct creader_cache_key {
295-
pub cnum: CrateNum,
296-
pub pos: usize,
297-
pub len: usize
298-
}
299-
300292
#[derive(Clone, PartialEq, RustcDecodable, RustcEncodable)]
301293
pub struct ItemVariances {
302294
pub types: VecPerParamSpace<Variance>,
@@ -562,6 +554,15 @@ pub enum vtable_origin<'tcx> {
562554
// expr to the associated trait ref.
563555
pub type ObjectCastMap<'tcx> = RefCell<NodeMap<ty::PolyTraitRef<'tcx>>>;
564556

557+
// Contains information needed to resolve types and (in the future) look up
558+
// the types of AST nodes.
559+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
560+
pub struct creader_cache_key {
561+
pub cnum: CrateNum,
562+
pub pos: usize,
563+
pub len: usize
564+
}
565+
565566
/// A restriction that certain types must be the same size. The use of
566567
/// `transmute` gives rise to these restrictions. These generally
567568
/// cannot be checked until trans; therefore, each call to `transmute`
@@ -827,6 +828,10 @@ pub struct ctxt<'tcx> {
827828

828829
/// Caches CoerceUnsized kinds for impls on custom types.
829830
pub custom_coerce_unsized_kinds: RefCell<DefIdMap<CustomCoerceUnsized>>,
831+
832+
/// Maps a cast expression to its kind. This is keyed on the
833+
/// *from* expression of the cast, not the cast itself.
834+
pub cast_kinds: RefCell<NodeMap<cast::CastKind>>,
830835
}
831836

832837
impl<'tcx> ctxt<'tcx> {
@@ -2817,6 +2822,7 @@ pub fn mk_ctxt<'tcx>(s: Session,
28172822
type_impls_sized_cache: RefCell::new(HashMap::new()),
28182823
const_qualif_map: RefCell::new(NodeMap()),
28192824
custom_coerce_unsized_kinds: RefCell::new(DefIdMap()),
2825+
cast_kinds: RefCell::new(NodeMap()),
28202826
}
28212827
}
28222828

src/librustc_lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl LintPass for TypeLimits {
206206
let (min, max) = int_ty_range(int_type);
207207
let negative = self.negated_expr_id == e.id;
208208

209-
if (negative && v > (min.abs() as u64)) ||
209+
if (negative && v > min.wrapping_neg() as u64) ||
210210
(!negative && v > (max.abs() as u64)) {
211211
cx.span_lint(OVERFLOWING_LITERALS, e.span,
212212
&*format!("literal out of range for {:?}", t));

0 commit comments

Comments
 (0)