Skip to content

Commit

Permalink
Require exact type equality + add tests
Browse files Browse the repository at this point in the history
+ Rebase fixes
  • Loading branch information
petrochenkov committed Dec 18, 2015
1 parent e0ceef5 commit 95fdaf2
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 43 deletions.
2 changes: 2 additions & 0 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax_ext/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions src/test/compile-fail/coerce-expect-unsized-ascribed.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 64 additions & 0 deletions src/test/compile-fail/type-ascription-precedence.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions src/test/compile-fail/type-ascription-soundness.rs
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 0 additions & 40 deletions src/test/run-pass/coerce-expect-unsized-ascribed.rs

This file was deleted.

45 changes: 45 additions & 0 deletions src/test/run-pass/type-ascription.rs
Original file line number Diff line number Diff line change
@@ -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]);
}

0 comments on commit 95fdaf2

Please sign in to comment.