-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Make casts follow the new RFC401 #24333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a172f40
Expose enum discriminant signedness
83acebc
Overhaul cast semantics and make them follow RFC401
9ee2335
Fix test fallout, and add some rather comprehensive tests.
d9b9f4e
fix conflicts
arielb1 de4b0e9
remove todo
arielb1 32fe2e3
Address review commets
27d2bd1
Make float -> int casts actually work
arielb1 3afd760
Fix translation of semi-constant if-statements
arielb1 e7e1fd2
Fix rebase conflicts
arielb1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -801,7 +801,6 @@ struct Foo<T: 'static> { | |
|
||
register_diagnostics! { | ||
E0011, | ||
E0012, | ||
E0014, | ||
E0016, | ||
E0017, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Helpers for handling cast expressions, used in both | ||
// typeck and trans. | ||
|
||
use middle::ty::{self, Ty}; | ||
|
||
use syntax::ast; | ||
|
||
/// Types that are represented as ints. | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub enum IntTy { | ||
U(ast::UintTy), | ||
I, | ||
CEnum, | ||
Bool, | ||
Char | ||
} | ||
|
||
// Valid types for the result of a non-coercion cast | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub enum CastTy<'tcx> { | ||
/// Various types that are represented as ints and handled mostly | ||
/// in the same way, merged for easier matching. | ||
Int(IntTy), | ||
/// Floating-Point types | ||
Float, | ||
/// Function Pointers | ||
FnPtr, | ||
/// Raw pointers | ||
Ptr(&'tcx ty::mt<'tcx>), | ||
/// References | ||
RPtr(&'tcx ty::mt<'tcx>), | ||
} | ||
|
||
/// Cast Kind. See RFC 401 (or librustc_typeck/check/cast.rs) | ||
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)] | ||
pub enum CastKind { | ||
CoercionCast, | ||
PtrPtrCast, | ||
PtrAddrCast, | ||
AddrPtrCast, | ||
NumericCast, | ||
EnumCast, | ||
PrimIntCast, | ||
U8CharCast, | ||
ArrayPtrCast, | ||
FnPtrPtrCast, | ||
FnPtrAddrCast | ||
} | ||
|
||
impl<'tcx> CastTy<'tcx> { | ||
pub fn from_ty(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>) | ||
-> Option<CastTy<'tcx>> { | ||
match t.sty { | ||
ty::ty_bool => Some(CastTy::Int(IntTy::Bool)), | ||
ty::ty_char => Some(CastTy::Int(IntTy::Char)), | ||
ty::ty_int(_) => Some(CastTy::Int(IntTy::I)), | ||
ty::ty_uint(u) => Some(CastTy::Int(IntTy::U(u))), | ||
ty::ty_float(_) => Some(CastTy::Float), | ||
ty::ty_enum(..) if ty::type_is_c_like_enum( | ||
tcx, t) => Some(CastTy::Int(IntTy::CEnum)), | ||
ty::ty_ptr(ref mt) => Some(CastTy::Ptr(mt)), | ||
ty::ty_rptr(_, ref mt) => Some(CastTy::RPtr(mt)), | ||
ty::ty_bare_fn(..) => Some(CastTy::FnPtr), | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ use session::Session; | |
use lint; | ||
use metadata::csearch; | ||
use middle; | ||
use middle::cast; | ||
use middle::check_const; | ||
use middle::const_eval; | ||
use middle::def::{self, DefMap, ExportMap}; | ||
|
@@ -288,15 +289,6 @@ pub struct field_ty { | |
pub origin: ast::DefId, // The DefId of the struct in which the field is declared. | ||
} | ||
|
||
// Contains information needed to resolve types and (in the future) look up | ||
// the types of AST nodes. | ||
#[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
pub struct creader_cache_key { | ||
pub cnum: CrateNum, | ||
pub pos: usize, | ||
pub len: usize | ||
} | ||
|
||
#[derive(Clone, PartialEq, RustcDecodable, RustcEncodable)] | ||
pub struct ItemVariances { | ||
pub types: VecPerParamSpace<Variance>, | ||
|
@@ -562,6 +554,15 @@ pub enum vtable_origin<'tcx> { | |
// expr to the associated trait ref. | ||
pub type ObjectCastMap<'tcx> = RefCell<NodeMap<ty::PolyTraitRef<'tcx>>>; | ||
|
||
// Contains information needed to resolve types and (in the future) look up | ||
// the types of AST nodes. | ||
#[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
pub struct creader_cache_key { | ||
pub cnum: CrateNum, | ||
pub pos: usize, | ||
pub len: usize | ||
} | ||
|
||
/// A restriction that certain types must be the same size. The use of | ||
/// `transmute` gives rise to these restrictions. These generally | ||
/// cannot be checked until trans; therefore, each call to `transmute` | ||
|
@@ -827,6 +828,10 @@ pub struct ctxt<'tcx> { | |
|
||
/// Caches CoerceUnsized kinds for impls on custom types. | ||
pub custom_coerce_unsized_kinds: RefCell<DefIdMap<CustomCoerceUnsized>>, | ||
|
||
/// Maps a cast expression to its kind. This is keyed on the | ||
/// *from* expression of the cast, not the cast itself. | ||
pub cast_kinds: RefCell<NodeMap<cast::CastKind>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because that's what I have in check/cast.rs |
||
} | ||
|
||
impl<'tcx> ctxt<'tcx> { | ||
|
@@ -2817,6 +2822,7 @@ pub fn mk_ctxt<'tcx>(s: Session, | |
type_impls_sized_cache: RefCell::new(HashMap::new()), | ||
const_qualif_map: RefCell::new(NodeMap()), | ||
custom_coerce_unsized_kinds: RefCell::new(DefIdMap()), | ||
cast_kinds: RefCell::new(NodeMap()), | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This enum could do with some explanation