From 42f95d055c2f22078f5c94c0d0ca229e1561ccb8 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Mon, 4 Mar 2013 18:03:21 -0800 Subject: [PATCH 1/5] Allow constant expressions in [Type * n]. --- src/librustc/middle/typeck/astconv.rs | 27 ++++++++++++++++++++++++--- src/libsyntax/ast.rs | 2 +- src/libsyntax/parse/parser.rs | 21 ++++----------------- src/libsyntax/print/pprust.rs | 6 +++--- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/librustc/middle/typeck/astconv.rs b/src/librustc/middle/typeck/astconv.rs index 8e0633c10cdfb..2a70ba7d4c129 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( } } } - 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/libsyntax/ast.rs b/src/libsyntax/ast.rs index e5fb2ad153cf1..e2e750e8903fb 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -916,7 +916,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/parse/parser.rs b/src/libsyntax/parse/parser.rs index 8a883b73a64a3..c7e93635d4c76 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 { + 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 71f3de174140c..ab7fe8dc063bd 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)); + word(s.s, fmt!("%?", v)); word(s.s, ~"]"); } ast::ty_mac(_) => { @@ -1015,7 +1015,7 @@ pub fn print_mac(s: @ps, m: ast::mac) { pub fn print_vstore(s: @ps, t: ast::vstore) { match t { - ast::vstore_fixed(Some(i)) => word(s.s, fmt!("%u", i)), + ast::vstore_fixed(Some(i)) => word(s.s, fmt!("%?", i)), ast::vstore_fixed(None) => word(s.s, ~"_"), ast::vstore_uniq => word(s.s, ~"~"), ast::vstore_box => word(s.s, ~"@"), @@ -1028,7 +1028,7 @@ pub fn print_vstore(s: @ps, t: ast::vstore) { pub fn print_expr_vstore(s: @ps, t: ast::expr_vstore) { match t { - ast::expr_vstore_fixed(Some(i)) => word(s.s, fmt!("%u", i)), + ast::expr_vstore_fixed(Some(i)) => word(s.s, fmt!("%?", i)), ast::expr_vstore_fixed(None) => word(s.s, ~"_"), ast::expr_vstore_uniq => word(s.s, ~"~"), ast::expr_vstore_box => word(s.s, ~"@"), From 787f5bb0dbbbd50f59848f6703bd89438e60053f Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 5 Mar 2013 17:43:37 -0800 Subject: [PATCH 2/5] Now actually allow using constants in those constant expressions for [T * n]. --- src/librustc/middle/lint.rs | 7 ++++++- src/libsyntax/fold.rs | 4 ++-- src/libsyntax/visit.rs | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index f2e736ac86d17..ccd7e35f817c0 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(v: visit::vt) -> visit::vt { - 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(v: visit::vt) -> visit::vt { + 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/libsyntax/fold.rs b/src/libsyntax/fold.rs index a8952f313a5b1..159b23f4f9997 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/visit.rs b/src/libsyntax/visit.rs index 6a0f1a2ec46b2..a159c98d21b27 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -246,7 +246,10 @@ pub fn visit_ty(t: @Ty, e: E, v: vt) { (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 => () } } From 8b66578e66957dfdbfed60634e0c7661213e442f Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 5 Mar 2013 17:45:12 -0800 Subject: [PATCH 3/5] Allow expressions that are not just a single value for repeated fixed length vector syntax. --- src/librustc/middle/typeck/check/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 752d9107d822a..7a1beeca51368 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}, From da64994f18cbeae9f0fd1527fa46347a0bcea7c5 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 5 Mar 2013 18:09:33 -0800 Subject: [PATCH 4/5] Fix pretty printer for fixed length vectors. --- src/libsyntax/ast.rs | 1 - src/libsyntax/print/pprust.rs | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index e2e750e8903fb..65eeff5bbab38 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), // [1,2,3,4] expr_vstore_uniq, // ~[1,2,3,4] expr_vstore_box, // @[1,2,3,4] diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index ab7fe8dc063bd..93583a1487a84 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!("%?", v)); + print_expr(s, v); word(s.s, ~"]"); } ast::ty_mac(_) => { @@ -1015,7 +1015,7 @@ pub fn print_mac(s: @ps, m: ast::mac) { pub fn print_vstore(s: @ps, t: ast::vstore) { match t { - ast::vstore_fixed(Some(i)) => word(s.s, fmt!("%?", i)), + ast::vstore_fixed(Some(i)) => word(s.s, fmt!("%u", i)), ast::vstore_fixed(None) => word(s.s, ~"_"), ast::vstore_uniq => word(s.s, ~"~"), ast::vstore_box => word(s.s, ~"@"), @@ -1028,7 +1028,7 @@ pub fn print_vstore(s: @ps, t: ast::vstore) { pub fn print_expr_vstore(s: @ps, t: ast::expr_vstore) { match t { - ast::expr_vstore_fixed(Some(i)) => word(s.s, fmt!("%?", i)), + ast::expr_vstore_fixed(Some(i)) => word(s.s, fmt!("%u", i)), ast::expr_vstore_fixed(None) => word(s.s, ~"_"), ast::expr_vstore_uniq => word(s.s, ~"~"), ast::expr_vstore_box => word(s.s, ~"@"), From d7d17dc14e653332848f7f6f994b34eb7fc923ec Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Wed, 6 Mar 2013 13:34:44 -0800 Subject: [PATCH 5/5] Add tests for const exprs in fixed vec length type and vec repeat. --- .../non-constant-expr-for-fixed-len-vec.rs | 17 +++++++++++++++++ .../non-constant-expr-for-vec-repeat.rs | 17 +++++++++++++++++ .../const-expr-in-fixed-length-vec.rs | 19 +++++++++++++++++++ src/test/run-pass/const-expr-in-vec-repeat.rs | 18 ++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 src/test/compile-fail/non-constant-expr-for-fixed-len-vec.rs create mode 100644 src/test/compile-fail/non-constant-expr-for-vec-repeat.rs create mode 100644 src/test/run-pass/const-expr-in-fixed-length-vec.rs create mode 100644 src/test/run-pass/const-expr-in-vec-repeat.rs 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 0000000000000..86262008ff943 --- /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 or the MIT license +// , 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 0000000000000..2727db9d0422e --- /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 or the MIT license +// , 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 0000000000000..aa5c4cbbc1d0f --- /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 or the MIT license +// , 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 0000000000000..76952ef730fa1 --- /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 or the MIT license +// , 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]; + +}