diff --git a/src/doc/reference.md b/src/doc/reference.md index a20d2571152..1033ce0df57 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -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 diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index eafbfe37e48..d4778dc165a 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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) => { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index dd1fe22a4eb..075360b9623 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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 diff --git a/src/libsyntax_ext/asm.rs b/src/libsyntax_ext/asm.rs index 12eabe0edfd..e5c6a17c78b 100644 --- a/src/libsyntax_ext/asm.rs +++ b/src/libsyntax_ext/asm.rs @@ -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; diff --git a/src/test/compile-fail/coerce-expect-unsized-ascribed.rs b/src/test/compile-fail/coerce-expect-unsized-ascribed.rs new file mode 100644 index 00000000000..ef65927fc5d --- /dev/null +++ b/src/test/compile-fail/coerce-expect-unsized-ascribed.rs @@ -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 or the MIT license +// , 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 _>; //~ ERROR mismatched types + let _ = box if true { false } else { true }: Box; //~ ERROR mismatched types + let _ = box match true { true => 'a', false => 'b' }: Box; //~ 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 _>; //~ ERROR mismatched types + + let _ = vec![ + Box::new(|x| (x as u8)), + box |x| (x as i16 as u8), + ]: Vec _>>; +} diff --git a/src/test/compile-fail/type-ascription-precedence.rs b/src/test/compile-fail/type-ascription-precedence.rs new file mode 100644 index 00000000000..bb7a8bc3ddf --- /dev/null +++ b/src/test/compile-fail/type-ascription-precedence.rs @@ -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 or the MIT license +// , 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 for S { + type Output = S; + fn add(self, _rhs: Z) -> S { panic!() } +} +impl Mul 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 +} diff --git a/src/test/compile-fail/type-ascription-soundness.rs b/src/test/compile-fail/type-ascription-soundness.rs new file mode 100644 index 00000000000..2d882e87ab8 --- /dev/null +++ b/src/test/compile-fail/type-ascription-soundness.rs @@ -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 or the MIT license +// , 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 +} diff --git a/src/test/run-pass/coerce-expect-unsized-ascribed.rs b/src/test/run-pass/coerce-expect-unsized-ascribed.rs deleted file mode 100644 index 33e9b424fea..00000000000 --- a/src/test/run-pass/coerce-expect-unsized-ascribed.rs +++ /dev/null @@ -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 or the MIT license -// , 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 _>; - let _ = box if true { false } else { true }: Box; - let _ = box match true { true => 'a', false => 'b' }: Box; - - 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 _>; - - let _ = vec![ - Box::new(|x| (x as u8)), - box |x| (x as i16 as u8), - ]: Vec _>>; -} diff --git a/src/test/run-pass/type-ascription.rs b/src/test/run-pass/type-ascription.rs new file mode 100644 index 00000000000..bca384c6471 --- /dev/null +++ b/src/test/run-pass/type-ascription.rs @@ -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 or the MIT license +// , 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 = vec![1, 2, 3]; // Lvalue type ascription + assert_eq!(v, [1u8, 2, 3]); +}