Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow constant expressions in [Type * n]. #5112

Merged
merged 5 commits into from
Mar 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 24 additions & 3 deletions src/librustc/middle/typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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},
Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
21 changes: 4 additions & 17 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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, &copy *self.token)
)
);
}
}
Some(self.parse_expr())
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand Down
5 changes: 4 additions & 1 deletion src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ()
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/compile-fail/non-constant-expr-for-fixed-len-vec.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
17 changes: 17 additions & 0 deletions src/test/compile-fail/non-constant-expr-for-vec-repeat.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
19 changes: 19 additions & 0 deletions src/test/run-pass/const-expr-in-fixed-length-vec.rs
Original file line number Diff line number Diff line change
@@ -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];

}
18 changes: 18 additions & 0 deletions src/test/run-pass/const-expr-in-vec-repeat.rs
Original file line number Diff line number Diff line change
@@ -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];

}