Skip to content

Commit 95fdaf2

Browse files
committed
Require exact type equality + add tests
+ Rebase fixes
1 parent e0ceef5 commit 95fdaf2

File tree

9 files changed

+188
-43
lines changed

9 files changed

+188
-43
lines changed

src/doc/reference.md

+2
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,8 @@ The currently implemented features of the reference compiler are:
23922392

23932393
* - `deprecated` - Allows using the `#[deprecated]` attribute.
23942394

2395+
* - `type_ascription` - Allows type ascription expressions `expr: Type`.
2396+
23952397
If a feature is promoted to a language feature, then all existing programs will
23962398
start to receive compilation warnings about `#![feature]` directives which enabled
23972399
the new feature (because the directive is no longer necessary). However, if a

src/librustc_typeck/check/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,14 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
26712671
}
26722672
}
26732673

2674+
fn check_expr_eq_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
2675+
expr: &'tcx hir::Expr,
2676+
expected: Ty<'tcx>) {
2677+
check_expr_with_unifier(
2678+
fcx, expr, ExpectHasType(expected), NoPreference,
2679+
|| demand::eqtype(fcx, expr.span, expected, fcx.expr_ty(expr)));
2680+
}
2681+
26742682
pub fn check_expr_has_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
26752683
expr: &'tcx hir::Expr,
26762684
expected: Ty<'tcx>) {
@@ -3527,7 +3535,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35273535
}
35283536
hir::ExprType(ref e, ref t) => {
35293537
let typ = fcx.to_ty(&**t);
3530-
check_expr_coercable_to_type(fcx, &**e, typ);
3538+
check_expr_eq_type(fcx, &**e, typ);
35313539
fcx.write_ty(id, typ);
35323540
}
35333541
hir::ExprVec(ref args) => {

src/libsyntax/parse/parser.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2791,7 +2791,8 @@ impl<'a> Parser<'a> {
27912791
continue
27922792
} else if op == AssocOp::Colon {
27932793
let rhs = try!(self.parse_ty());
2794-
lhs = self.mk_expr(lhs.span.lo, rhs.span.hi, ExprType(lhs, rhs));
2794+
lhs = self.mk_expr(lhs.span.lo, rhs.span.hi,
2795+
ExprType(lhs, rhs), None);
27952796
continue
27962797
} else if op == AssocOp::DotDot {
27972798
// If we didn’t have to handle `x..`, it would be pretty easy to generalise

src/libsyntax_ext/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::codemap::Span;
1919
use syntax::ext::base;
2020
use syntax::ext::base::*;
2121
use syntax::feature_gate;
22-
use syntax::parse::token::{intern, InternedString};
22+
use syntax::parse::token::intern;
2323
use syntax::parse::{self, token};
2424
use syntax::ptr::P;
2525
use syntax::ast::AsmDialect;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2014 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+
// A version of coerce-expect-unsized that uses type ascription.
12+
// Doesn't work so far, but supposed to work eventually
13+
14+
#![feature(box_syntax, type_ascription)]
15+
16+
use std::fmt::Debug;
17+
18+
pub fn main() {
19+
let _ = box { [1, 2, 3] }: Box<[i32]>; //~ ERROR mismatched types
20+
let _ = box if true { [1, 2, 3] } else { [1, 3, 4] }: Box<[i32]>; //~ ERROR mismatched types
21+
let _ = box match true { true => [1, 2, 3], false => [1, 3, 4] }: Box<[i32]>;
22+
//~^ ERROR mismatched types
23+
let _ = box { |x| (x as u8) }: Box<Fn(i32) -> _>; //~ ERROR mismatched types
24+
let _ = box if true { false } else { true }: Box<Debug>; //~ ERROR mismatched types
25+
let _ = box match true { true => 'a', false => 'b' }: Box<Debug>; //~ ERROR mismatched types
26+
27+
let _ = &{ [1, 2, 3] }: &[i32]; //~ ERROR mismatched types
28+
let _ = &if true { [1, 2, 3] } else { [1, 3, 4] }: &[i32]; //~ ERROR mismatched types
29+
let _ = &match true { true => [1, 2, 3], false => [1, 3, 4] }: &[i32];
30+
//~^ ERROR mismatched types
31+
let _ = &{ |x| (x as u8) }: &Fn(i32) -> _; //~ ERROR mismatched types
32+
let _ = &if true { false } else { true }: &Debug; //~ ERROR mismatched types
33+
let _ = &match true { true => 'a', false => 'b' }: &Debug; //~ ERROR mismatched types
34+
35+
let _ = Box::new([1, 2, 3]): Box<[i32]>; //~ ERROR mismatched types
36+
let _ = Box::new(|x| (x as u8)): Box<Fn(i32) -> _>; //~ ERROR mismatched types
37+
38+
let _ = vec![
39+
Box::new(|x| (x as u8)),
40+
box |x| (x as i16 as u8),
41+
]: Vec<Box<Fn(i32) -> _>>;
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// Operator precedence of type ascription
12+
// Type ascription has very high precedence, the same as operator `as`
13+
14+
#![feature(type_ascription)]
15+
16+
use std::ops::*;
17+
18+
struct S;
19+
struct Z;
20+
21+
impl Add<Z> for S {
22+
type Output = S;
23+
fn add(self, _rhs: Z) -> S { panic!() }
24+
}
25+
impl Mul<Z> for S {
26+
type Output = S;
27+
fn mul(self, _rhs: Z) -> S { panic!() }
28+
}
29+
impl Neg for S {
30+
type Output = Z;
31+
fn neg(self) -> Z { panic!() }
32+
}
33+
impl Deref for S {
34+
type Target = Z;
35+
fn deref(&self) -> &Z { panic!() }
36+
}
37+
38+
fn main() {
39+
&S: &S; // OK
40+
(&S): &S; // OK
41+
&(S: &S); //~ ERROR mismatched types
42+
43+
*S: Z; // OK
44+
(*S): Z; // OK
45+
*(S: Z); //~ ERROR mismatched types
46+
//~^ ERROR type `Z` cannot be dereferenced
47+
48+
-S: Z; // OK
49+
(-S): Z; // OK
50+
-(S: Z); //~ ERROR mismatched types
51+
//~^ ERROR cannot apply unary operator `-` to type `Z`
52+
53+
S + Z: Z; // OK
54+
S + (Z: Z); // OK
55+
(S + Z): Z; //~ ERROR mismatched types
56+
57+
S * Z: Z; // OK
58+
S * (Z: Z); // OK
59+
(S * Z): Z; //~ ERROR mismatched types
60+
61+
S .. S: S; // OK
62+
S .. (S: S); // OK
63+
(S .. S): S; //~ ERROR mismatched types
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// Type ascription doesn't lead to unsoundness
12+
13+
#![feature(type_ascription)]
14+
15+
fn main() {
16+
let arr = &[1u8, 2, 3];
17+
let ref x = arr: &[u8]; //~ ERROR mismatched types
18+
let ref mut x = arr: &[u8]; //~ ERROR mismatched types
19+
match arr: &[u8] { //~ ERROR mismatched types
20+
ref x => {}
21+
}
22+
let _len = (arr: &[u8]).len(); //~ ERROR mismatched types
23+
}

src/test/run-pass/coerce-expect-unsized-ascribed.rs

-40
This file was deleted.

src/test/run-pass/type-ascription.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
// Type ascription doesn't lead to unsoundness
12+
13+
#![feature(type_ascription)]
14+
15+
use std::mem;
16+
17+
const C1: u8 = 10: u8;
18+
const C2: [u8; 1: usize] = [1];
19+
20+
struct S {
21+
a: u8
22+
}
23+
24+
fn main() {
25+
assert_eq!(C1.into(): i32, 10);
26+
assert_eq!(C2[0], 1);
27+
28+
let s = S { a: 10: u8 };
29+
let arr = &[1u8, 2, 3];
30+
31+
let mut v = arr.iter().cloned().collect(): Vec<_>;
32+
v.push(4);
33+
assert_eq!(v, [1, 2, 3, 4]);
34+
35+
let a = 1: u8;
36+
let b = a.into(): u16;
37+
assert_eq!(v[a.into(): usize], 2);
38+
assert_eq!(mem::size_of_val(&a), 1);
39+
assert_eq!(mem::size_of_val(&b), 2);
40+
assert_eq!(b, 1: u16);
41+
42+
let mut v = Vec::new();
43+
v: Vec<u8> = vec![1, 2, 3]; // Lvalue type ascription
44+
assert_eq!(v, [1u8, 2, 3]);
45+
}

0 commit comments

Comments
 (0)