mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
Require exact type equality + add tests
+ Rebase fixes
This commit is contained in:
parent
e0ceef5a9e
commit
95fdaf2375
@ -2392,6 +2392,8 @@ The currently implemented features of the reference compiler are:
|
||||
|
||||
* - `deprecated` - Allows using the `#[deprecated]` attribute.
|
||||
|
||||
* - `type_ascription` - Allows type ascription expressions `expr: Type`.
|
||||
|
||||
If a feature is promoted to a language feature, then all existing programs will
|
||||
start to receive compilation warnings about `#![feature]` directives which enabled
|
||||
the new feature (because the directive is no longer necessary). However, if a
|
||||
|
@ -2671,6 +2671,14 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
|
||||
}
|
||||
}
|
||||
|
||||
fn check_expr_eq_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
|
||||
expr: &'tcx hir::Expr,
|
||||
expected: Ty<'tcx>) {
|
||||
check_expr_with_unifier(
|
||||
fcx, expr, ExpectHasType(expected), NoPreference,
|
||||
|| demand::eqtype(fcx, expr.span, expected, fcx.expr_ty(expr)));
|
||||
}
|
||||
|
||||
pub fn check_expr_has_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
|
||||
expr: &'tcx hir::Expr,
|
||||
expected: Ty<'tcx>) {
|
||||
@ -3527,7 +3535,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
|
||||
}
|
||||
hir::ExprType(ref e, ref t) => {
|
||||
let typ = fcx.to_ty(&**t);
|
||||
check_expr_coercable_to_type(fcx, &**e, typ);
|
||||
check_expr_eq_type(fcx, &**e, typ);
|
||||
fcx.write_ty(id, typ);
|
||||
}
|
||||
hir::ExprVec(ref args) => {
|
||||
|
@ -2791,7 +2791,8 @@ impl<'a> Parser<'a> {
|
||||
continue
|
||||
} else if op == AssocOp::Colon {
|
||||
let rhs = try!(self.parse_ty());
|
||||
lhs = self.mk_expr(lhs.span.lo, rhs.span.hi, ExprType(lhs, rhs));
|
||||
lhs = self.mk_expr(lhs.span.lo, rhs.span.hi,
|
||||
ExprType(lhs, rhs), None);
|
||||
continue
|
||||
} else if op == AssocOp::DotDot {
|
||||
// If we didn’t have to handle `x..`, it would be pretty easy to generalise
|
||||
|
@ -19,7 +19,7 @@ use syntax::codemap::Span;
|
||||
use syntax::ext::base;
|
||||
use syntax::ext::base::*;
|
||||
use syntax::feature_gate;
|
||||
use syntax::parse::token::{intern, InternedString};
|
||||
use syntax::parse::token::intern;
|
||||
use syntax::parse::{self, token};
|
||||
use syntax::ptr::P;
|
||||
use syntax::ast::AsmDialect;
|
||||
|
42
src/test/compile-fail/coerce-expect-unsized-ascribed.rs
Normal file
42
src/test/compile-fail/coerce-expect-unsized-ascribed.rs
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2014 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.
|
||||
|
||||
// A version of coerce-expect-unsized that uses type ascription.
|
||||
// Doesn't work so far, but supposed to work eventually
|
||||
|
||||
#![feature(box_syntax, type_ascription)]
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub fn main() {
|
||||
let _ = box { [1, 2, 3] }: Box<[i32]>; //~ ERROR mismatched types
|
||||
let _ = box if true { [1, 2, 3] } else { [1, 3, 4] }: Box<[i32]>; //~ ERROR mismatched types
|
||||
let _ = box match true { true => [1, 2, 3], false => [1, 3, 4] }: Box<[i32]>;
|
||||
//~^ ERROR mismatched types
|
||||
let _ = box { |x| (x as u8) }: Box<Fn(i32) -> _>; //~ ERROR mismatched types
|
||||
let _ = box if true { false } else { true }: Box<Debug>; //~ ERROR mismatched types
|
||||
let _ = box match true { true => 'a', false => 'b' }: Box<Debug>; //~ ERROR mismatched types
|
||||
|
||||
let _ = &{ [1, 2, 3] }: &[i32]; //~ ERROR mismatched types
|
||||
let _ = &if true { [1, 2, 3] } else { [1, 3, 4] }: &[i32]; //~ ERROR mismatched types
|
||||
let _ = &match true { true => [1, 2, 3], false => [1, 3, 4] }: &[i32];
|
||||
//~^ ERROR mismatched types
|
||||
let _ = &{ |x| (x as u8) }: &Fn(i32) -> _; //~ ERROR mismatched types
|
||||
let _ = &if true { false } else { true }: &Debug; //~ ERROR mismatched types
|
||||
let _ = &match true { true => 'a', false => 'b' }: &Debug; //~ ERROR mismatched types
|
||||
|
||||
let _ = Box::new([1, 2, 3]): Box<[i32]>; //~ ERROR mismatched types
|
||||
let _ = Box::new(|x| (x as u8)): Box<Fn(i32) -> _>; //~ ERROR mismatched types
|
||||
|
||||
let _ = vec![
|
||||
Box::new(|x| (x as u8)),
|
||||
box |x| (x as i16 as u8),
|
||||
]: Vec<Box<Fn(i32) -> _>>;
|
||||
}
|
64
src/test/compile-fail/type-ascription-precedence.rs
Normal file
64
src/test/compile-fail/type-ascription-precedence.rs
Normal file
@ -0,0 +1,64 @@
|
||||
// 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.
|
||||
|
||||
// Operator precedence of type ascription
|
||||
// Type ascription has very high precedence, the same as operator `as`
|
||||
|
||||
#![feature(type_ascription)]
|
||||
|
||||
use std::ops::*;
|
||||
|
||||
struct S;
|
||||
struct Z;
|
||||
|
||||
impl Add<Z> for S {
|
||||
type Output = S;
|
||||
fn add(self, _rhs: Z) -> S { panic!() }
|
||||
}
|
||||
impl Mul<Z> for S {
|
||||
type Output = S;
|
||||
fn mul(self, _rhs: Z) -> S { panic!() }
|
||||
}
|
||||
impl Neg for S {
|
||||
type Output = Z;
|
||||
fn neg(self) -> Z { panic!() }
|
||||
}
|
||||
impl Deref for S {
|
||||
type Target = Z;
|
||||
fn deref(&self) -> &Z { panic!() }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
&S: &S; // OK
|
||||
(&S): &S; // OK
|
||||
&(S: &S); //~ ERROR mismatched types
|
||||
|
||||
*S: Z; // OK
|
||||
(*S): Z; // OK
|
||||
*(S: Z); //~ ERROR mismatched types
|
||||
//~^ ERROR type `Z` cannot be dereferenced
|
||||
|
||||
-S: Z; // OK
|
||||
(-S): Z; // OK
|
||||
-(S: Z); //~ ERROR mismatched types
|
||||
//~^ ERROR cannot apply unary operator `-` to type `Z`
|
||||
|
||||
S + Z: Z; // OK
|
||||
S + (Z: Z); // OK
|
||||
(S + Z): Z; //~ ERROR mismatched types
|
||||
|
||||
S * Z: Z; // OK
|
||||
S * (Z: Z); // OK
|
||||
(S * Z): Z; //~ ERROR mismatched types
|
||||
|
||||
S .. S: S; // OK
|
||||
S .. (S: S); // OK
|
||||
(S .. S): S; //~ ERROR mismatched types
|
||||
}
|
23
src/test/compile-fail/type-ascription-soundness.rs
Normal file
23
src/test/compile-fail/type-ascription-soundness.rs
Normal file
@ -0,0 +1,23 @@
|
||||
// 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.
|
||||
|
||||
// Type ascription doesn't lead to unsoundness
|
||||
|
||||
#![feature(type_ascription)]
|
||||
|
||||
fn main() {
|
||||
let arr = &[1u8, 2, 3];
|
||||
let ref x = arr: &[u8]; //~ ERROR mismatched types
|
||||
let ref mut x = arr: &[u8]; //~ ERROR mismatched types
|
||||
match arr: &[u8] { //~ ERROR mismatched types
|
||||
ref x => {}
|
||||
}
|
||||
let _len = (arr: &[u8]).len(); //~ ERROR mismatched types
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
// Copyright 2014 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.
|
||||
|
||||
#![allow(unknown_features)]
|
||||
#![feature(box_syntax)]
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
// A version of coerce-expect-unsized that uses type ascription.
|
||||
|
||||
pub fn main() {
|
||||
let _ = box { [1, 2, 3] }: Box<[i32]>;
|
||||
let _ = box if true { [1, 2, 3] } else { [1, 3, 4] }: Box<[i32]>;
|
||||
let _ = box match true { true => [1, 2, 3], false => [1, 3, 4] }: Box<[i32]>;
|
||||
let _ = box { |x| (x as u8) }: Box<Fn(i32) -> _>;
|
||||
let _ = box if true { false } else { true }: Box<Debug>;
|
||||
let _ = box match true { true => 'a', false => 'b' }: Box<Debug>;
|
||||
|
||||
let _ = &{ [1, 2, 3] }: &[i32];
|
||||
let _ = &if true { [1, 2, 3] } else { [1, 3, 4] }: &[i32];
|
||||
let _ = &match true { true => [1, 2, 3], false => [1, 3, 4] }: &[i32];
|
||||
let _ = &{ |x| (x as u8) }: &Fn(i32) -> _;
|
||||
let _ = &if true { false } else { true }: &Debug;
|
||||
let _ = &match true { true => 'a', false => 'b' }: &Debug;
|
||||
|
||||
let _ = Box::new([1, 2, 3]): Box<[i32]>;
|
||||
let _ = Box::new(|x| (x as u8)): Box<Fn(i32) -> _>;
|
||||
|
||||
let _ = vec![
|
||||
Box::new(|x| (x as u8)),
|
||||
box |x| (x as i16 as u8),
|
||||
]: Vec<Box<Fn(i32) -> _>>;
|
||||
}
|
45
src/test/run-pass/type-ascription.rs
Normal file
45
src/test/run-pass/type-ascription.rs
Normal file
@ -0,0 +1,45 @@
|
||||
// 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.
|
||||
|
||||
// Type ascription doesn't lead to unsoundness
|
||||
|
||||
#![feature(type_ascription)]
|
||||
|
||||
use std::mem;
|
||||
|
||||
const C1: u8 = 10: u8;
|
||||
const C2: [u8; 1: usize] = [1];
|
||||
|
||||
struct S {
|
||||
a: u8
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(C1.into(): i32, 10);
|
||||
assert_eq!(C2[0], 1);
|
||||
|
||||
let s = S { a: 10: u8 };
|
||||
let arr = &[1u8, 2, 3];
|
||||
|
||||
let mut v = arr.iter().cloned().collect(): Vec<_>;
|
||||
v.push(4);
|
||||
assert_eq!(v, [1, 2, 3, 4]);
|
||||
|
||||
let a = 1: u8;
|
||||
let b = a.into(): u16;
|
||||
assert_eq!(v[a.into(): usize], 2);
|
||||
assert_eq!(mem::size_of_val(&a), 1);
|
||||
assert_eq!(mem::size_of_val(&b), 2);
|
||||
assert_eq!(b, 1: u16);
|
||||
|
||||
let mut v = Vec::new();
|
||||
v: Vec<u8> = vec![1, 2, 3]; // Lvalue type ascription
|
||||
assert_eq!(v, [1u8, 2, 3]);
|
||||
}
|
Loading…
Reference in New Issue
Block a user