Skip to content

Commit 3157c3e

Browse files
committed
auto merge of #13715 : nick29581/rust/unsized-assign2, r=nikomatsakis
Closes #13376.
2 parents 70647cc + a08198b commit 3157c3e

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

src/librustc/middle/kind.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax::ast::*;
2020
use syntax::attr;
2121
use syntax::codemap::Span;
2222
use syntax::owned_slice::OwnedSlice;
23-
use syntax::print::pprust::expr_to_str;
23+
use syntax::print::pprust::{expr_to_str,path_to_str};
2424
use syntax::{visit,ast_util};
2525
use syntax::visit::Visitor;
2626

@@ -63,9 +63,14 @@ impl<'a> Visitor<()> for Context<'a> {
6363
fn visit_ty(&mut self, t: &Ty, _: ()) {
6464
check_ty(self, t);
6565
}
66+
6667
fn visit_item(&mut self, i: &Item, _: ()) {
6768
check_item(self, i);
6869
}
70+
71+
fn visit_pat(&mut self, p: &Pat, _: ()) {
72+
check_pat(self, p);
73+
}
6974
}
7075

7176
pub fn check_crate(tcx: &ty::ctxt,
@@ -551,3 +556,38 @@ pub fn check_cast_for_escaping_regions(
551556
}
552557
}
553558
}
559+
560+
// Ensure that `ty` has a statically known size (i.e., it has the `Sized` bound).
561+
fn check_sized(tcx: &ty::ctxt, ty: ty::t, name: ~str, sp: Span) {
562+
if !ty::type_is_sized(tcx, ty) {
563+
tcx.sess.span_err(sp, format!("variable `{}` has dynamically sized type `{}`",
564+
name, ty_to_str(tcx, ty)));
565+
}
566+
}
567+
568+
// Check that any variables in a pattern have types with statically known size.
569+
fn check_pat(cx: &mut Context, pat: &Pat) {
570+
let var_name = match pat.node {
571+
PatWild => Some("_".to_owned()),
572+
PatIdent(_, ref path, _) => Some(path_to_str(path)),
573+
_ => None
574+
};
575+
576+
match var_name {
577+
Some(name) => {
578+
let types = cx.tcx.node_types.borrow();
579+
let ty = types.find(&(pat.id as uint));
580+
match ty {
581+
Some(ty) => {
582+
debug!("kind: checking sized-ness of variable {}: {}",
583+
name, ty_to_str(cx.tcx, *ty));
584+
check_sized(cx.tcx, *ty, name, pat.span);
585+
}
586+
None => {} // extern fn args
587+
}
588+
}
589+
None => {}
590+
}
591+
592+
visit::walk_pat(cx, pat, ());
593+
}

src/test/compile-fail/unsized5.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,7 @@
99
// except according to those terms.
1010
#![feature(struct_variant)]
1111

12-
// Test `type` types not allowed in fields or local variables.
13-
14-
/*trait T for type {}
15-
16-
fn f5<type X>(x: &X) {
17-
let _: X; // ERROR local variable with dynamically sized type X
18-
let _: (int, (X, int)); // ERROR local variable with dynamically sized type (int,(X,int))
19-
}
20-
fn f6<type X: T>(x: &X) {
21-
let _: X; // ERROR local variable with dynamically sized type X
22-
let _: (int, (X, int)); // ERROR local variable with dynamically sized type (int,(X,int))
23-
}*/
12+
// Test `type` types not allowed in fields.
2413

2514
struct S1<type X> {
2615
f1: X, //~ ERROR type `f1` is dynamically sized. dynamically sized types may only appear as the

src/test/compile-fail/unsized6.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
// Test `type` local variables.
12+
13+
trait T for type {}
14+
15+
fn f1<type X>(x: &X) {
16+
let _: X; //~ERROR variable `_` has dynamically sized type `X`
17+
let _: (int, (X, int)); //~ERROR variable `_` has dynamically sized type `(int,(X,int))`
18+
let y: X; //~ERROR variable `y` has dynamically sized type `X`
19+
let y: (int, (X, int)); //~ERROR variable `y` has dynamically sized type `(int,(X,int))`
20+
}
21+
fn f2<type X: T>(x: &X) {
22+
let _: X; //~ERROR variable `_` has dynamically sized type `X`
23+
let _: (int, (X, int)); //~ERROR variable `_` has dynamically sized type `(int,(X,int))`
24+
let y: X; //~ERROR variable `y` has dynamically sized type `X`
25+
let y: (int, (X, int)); //~ERROR variable `y` has dynamically sized type `(int,(X,int))`
26+
}
27+
28+
fn f3<type X>(x1: ~X, x2: ~X, x3: ~X) {
29+
let y: X = *x1; //~ERROR variable `y` has dynamically sized type `X`
30+
let y = *x2; //~ERROR variable `y` has dynamically sized type `X`
31+
let (y, z) = (*x3, 4); //~ERROR variable `y` has dynamically sized type `X`
32+
}
33+
fn f4<type X: T>(x1: ~X, x2: ~X, x3: ~X) {
34+
let y: X = *x1; //~ERROR variable `y` has dynamically sized type `X`
35+
let y = *x2; //~ERROR variable `y` has dynamically sized type `X`
36+
let (y, z) = (*x3, 4); //~ERROR variable `y` has dynamically sized type `X`
37+
}
38+
39+
fn g1<type X>(x: X) {} //~ERROR variable `x` has dynamically sized type `X`
40+
fn g2<type X: T>(x: X) {} //~ERROR variable `x` has dynamically sized type `X`
41+
42+
pub fn main() {
43+
}

0 commit comments

Comments
 (0)