diff options
| author | bors <bors@rust-lang.org> | 2013-03-19 09:52:17 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-03-19 09:52:17 -0700 |
| commit | 3c84bac9462ae853b32f55fcaca2613a7e392d41 (patch) | |
| tree | 30290aecec90bcffd91a5d905256cd755e583bbe | |
| parent | e67448d397ed8f468170d6fba95ceae081ece624 (diff) | |
| parent | d7d17dc14e653332848f7f6f994b34eb7fc923ec (diff) | |
auto merge of #5112 : luqmana/rust/3469, r=graydon
So this is a partial fix for #3469. Partial because it only works for simple constant expressions like `32/2` and `2+2` and not for any actual constants. For example: ``` const FOO: uint = 2+2; let v: [int * FOO]; ``` results in: ``` error: expected constant expr for vector length: Non-constant path in constant expr ``` This seems to be because at the point of error (`typeck::astconv`) the def_map doesn't contain the constant and thus it can't lookup the actual expression (`2+2` in this case). So, feedback on what I have so far and suggestions for how to address the constant issue?
| -rw-r--r-- | src/librustc/middle/lint.rs | 7 | ||||
| -rw-r--r-- | src/librustc/middle/typeck/astconv.rs | 27 | ||||
| -rw-r--r-- | src/librustc/middle/typeck/check/mod.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/ast.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 21 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 5 | ||||
| -rw-r--r-- | src/test/compile-fail/non-constant-expr-for-fixed-len-vec.rs | 17 | ||||
| -rw-r--r-- | src/test/compile-fail/non-constant-expr-for-vec-repeat.rs | 17 | ||||
| -rw-r--r-- | src/test/run-pass/const-expr-in-fixed-length-vec.rs | 19 | ||||
| -rw-r--r-- | src/test/run-pass/const-expr-in-vec-repeat.rs | 18 |
12 files changed, 115 insertions, 29 deletions
diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index f2e736ac86d..ccd7e35f817 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -498,7 +498,12 @@ fn check_item(i: @ast::item, cx: ty::ctxt) { // not traverse into subitems, since that is handled by the outer // lint visitor. fn item_stopping_visitor<E>(v: visit::vt<E>) -> visit::vt<E> { - visit::mk_vt(@visit::Visitor {visit_item: |_i, _e, _v| { },.. **v}) + visit::mk_vt(@visit::Visitor {visit_item: |_i, _e, _v| { }, + .. **(ty_stopping_visitor(v))}) +} + +fn ty_stopping_visitor<E>(v: visit::vt<E>) -> visit::vt<E> { + visit::mk_vt(@visit::Visitor {visit_ty: |_t, _e, _v| { },.. **v}) } fn check_item_while_true(cx: ty::ctxt, it: @ast::item) { diff --git a/src/librustc/middle/typeck/astconv.rs b/src/librustc/middle/typeck/astconv.rs index 8e0633c10cd..2a70ba7d4c1 100644 --- a/src/librustc/middle/typeck/astconv.rs +++ b/src/librustc/middle/typeck/astconv.rs @@ -54,6 +54,7 @@ use core::prelude::*; +use middle::const_eval; use middle::ty::{arg, field, substs}; use middle::ty::{ty_param_substs_and_ty}; use middle::ty; @@ -412,9 +413,29 @@ pub fn ast_ty_to_ty<AC:AstConv, RS:region_scope + Copy + Durable>( } } } - ast::ty_fixed_length_vec(a_mt, u) => { - ty::mk_evec(tcx, ast_mt_to_mt(self, rscope, a_mt), - ty::vstore_fixed(u)) + ast::ty_fixed_length_vec(a_mt, e) => { + match const_eval::eval_const_expr_partial(tcx, e) { + Ok(ref r) => { + match *r { + const_eval::const_int(i) => + ty::mk_evec(tcx, ast_mt_to_mt(self, rscope, a_mt), + ty::vstore_fixed(i as uint)), + const_eval::const_uint(i) => + ty::mk_evec(tcx, ast_mt_to_mt(self, rscope, a_mt), + ty::vstore_fixed(i as uint)), + _ => { + tcx.sess.span_fatal( + ast_ty.span, ~"expected constant expr for vector length"); + } + } + } + Err(ref r) => { + tcx.sess.span_fatal( + ast_ty.span, + fmt!("expected constant expr for vector length: %s", + *r)); + } + } } ast::ty_infer => { // ty_infer should only appear as the type of arguments or return diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 752d9107d82..7a1beeca513 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -2169,7 +2169,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt, } ast::expr_repeat(element, count_expr, mutbl) => { let count = ty::eval_repeat_count(tcx, count_expr); - fcx.write_ty(count_expr.id, ty::mk_uint(tcx)); + check_expr_with_hint(fcx, count_expr, ty::mk_uint(tcx)); let tt = ast_expr_vstore_to_vstore(fcx, ev, count, vst); let t: ty::t = fcx.infcx().next_ty_var(); bot |= check_expr_has_type(fcx, element, t); @@ -2537,7 +2537,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt, } ast::expr_repeat(element, count_expr, mutbl) => { let count = ty::eval_repeat_count(tcx, count_expr); - fcx.write_ty(count_expr.id, ty::mk_uint(tcx)); + check_expr_with_hint(fcx, count_expr, ty::mk_uint(tcx)); let t: ty::t = fcx.infcx().next_ty_var(); bot |= check_expr_has_type(fcx, element, t); let t = ty::mk_evec(tcx, ty::mt {ty: t, mutbl: mutbl}, diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index e5fb2ad153c..65eeff5bbab 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -386,7 +386,6 @@ pub enum vstore { #[auto_decode] #[deriving_eq] pub enum expr_vstore { - // FIXME (#3469): Change uint to @expr (actually only constant exprs) expr_vstore_fixed(Option<uint>), // [1,2,3,4] expr_vstore_uniq, // ~[1,2,3,4] expr_vstore_box, // @[1,2,3,4] @@ -916,7 +915,7 @@ pub enum ty_ { ty_box(mt), ty_uniq(mt), ty_vec(mt), - ty_fixed_length_vec(mt, uint), + ty_fixed_length_vec(mt, @expr), ty_ptr(mt), ty_rptr(Option<@Lifetime>, mt), ty_closure(@TyClosure), diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index a8952f313a5..159b23f4f99 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -622,10 +622,10 @@ pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ { } ty_tup(ref tys) => ty_tup(tys.map(|ty| fld.fold_ty(*ty))), ty_path(path, id) => ty_path(fld.fold_path(path), fld.new_id(id)), - ty_fixed_length_vec(ref mt, vs) => { + ty_fixed_length_vec(ref mt, e) => { ty_fixed_length_vec( fold_mt(mt, fld), - vs + fld.fold_expr(e) ) } ty_mac(ref mac) => ty_mac(fold_mac(*mac)) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 8a883b73a64..c7e93635d4c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -642,7 +642,8 @@ pub impl Parser { self.obsolete(*self.last_span, ObsoleteMutVector); } - // Parse the `* 3` in `[ int * 3 ]` + // Parse the `* e` in `[ int * e ]` + // where `e` is a const expression let t = match self.maybe_parse_fixed_vstore_with_star() { None => ty_vec(mt), Some(suffix) => ty_fixed_length_vec(mt, suffix) @@ -814,23 +815,9 @@ pub impl Parser { }) } - fn maybe_parse_fixed_vstore_with_star(&self) -> Option<uint> { + fn maybe_parse_fixed_vstore_with_star(&self) -> Option<@ast::expr> { if self.eat(&token::BINOP(token::STAR)) { - match *self.token { - token::LIT_INT_UNSUFFIXED(i) if i >= 0i64 => { - self.bump(); - Some(i as uint) - } - _ => { - self.fatal( - fmt!( - "expected integral vector length \ - but found `%s`", - token_to_str(self.reader, © *self.token) - ) - ); - } - } + Some(self.parse_expr()) } else { None } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 71f3de17414..93583a1487a 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -425,7 +425,7 @@ pub fn print_type_ex(s: @ps, &&ty: @ast::Ty, print_colons: bool) { } print_type(s, mt.ty); word(s.s, ~" * "); - word(s.s, fmt!("%u", v)); + print_expr(s, v); word(s.s, ~"]"); } ast::ty_mac(_) => { diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 6a0f1a2ec46..a159c98d21b 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -246,7 +246,10 @@ pub fn visit_ty<E>(t: @Ty, e: E, v: vt<E>) { (v.visit_ty)(f.decl.output, e, v); }, ty_path(p, _) => visit_path(p, e, v), - ty_fixed_length_vec(ref mt, _) => (v.visit_ty)(mt.ty, e, v), + ty_fixed_length_vec(ref mt, ex) => { + (v.visit_ty)(mt.ty, e, v); + (v.visit_expr)(ex, e, v); + }, ty_nil | ty_bot | ty_mac(_) | ty_infer => () } } diff --git a/src/test/compile-fail/non-constant-expr-for-fixed-len-vec.rs b/src/test/compile-fail/non-constant-expr-for-fixed-len-vec.rs new file mode 100644 index 00000000000..86262008ff9 --- /dev/null +++ b/src/test/compile-fail/non-constant-expr-for-fixed-len-vec.rs @@ -0,0 +1,17 @@ +// Copyright 2012-2013 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. + +// Check that non-constant exprs do fail as count in fixed length vec type + +fn main() { + fn bar(n: int) { + let _x: [int * n]; //~ ERROR expected constant expr for vector length: Non-constant path in constant expr + } +} diff --git a/src/test/compile-fail/non-constant-expr-for-vec-repeat.rs b/src/test/compile-fail/non-constant-expr-for-vec-repeat.rs new file mode 100644 index 00000000000..2727db9d042 --- /dev/null +++ b/src/test/compile-fail/non-constant-expr-for-vec-repeat.rs @@ -0,0 +1,17 @@ +// Copyright 2012-2013 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. + +// Check that non constant exprs fail for vector repeat syntax + +fn main() { + fn bar(n: int) { + let _x = [0, ..n]; //~ ERROR expected constant integer for repeat count but found variable + } +} diff --git a/src/test/run-pass/const-expr-in-fixed-length-vec.rs b/src/test/run-pass/const-expr-in-fixed-length-vec.rs new file mode 100644 index 00000000000..aa5c4cbbc1d --- /dev/null +++ b/src/test/run-pass/const-expr-in-fixed-length-vec.rs @@ -0,0 +1,19 @@ +// Copyright 2012-2013 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. + +// Check that constant expressions can be used for declaring the +// type of a fixed length vector. + +fn main() { + + const FOO: int = 2; + let _v: [int * FOO*3]; + +} diff --git a/src/test/run-pass/const-expr-in-vec-repeat.rs b/src/test/run-pass/const-expr-in-vec-repeat.rs new file mode 100644 index 00000000000..76952ef730f --- /dev/null +++ b/src/test/run-pass/const-expr-in-vec-repeat.rs @@ -0,0 +1,18 @@ +// Copyright 2012-2013 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. + +// Check that constant expressions can be used in vec repeat syntax. + +fn main() { + + const FOO: int = 2; + let _v = [0, ..FOO*3*2/2]; + +} |
