Skip to content

Commit 3c84bac

Browse files
committed
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?
2 parents e67448d + d7d17dc commit 3c84bac

File tree

12 files changed

+115
-29
lines changed

12 files changed

+115
-29
lines changed

src/librustc/middle/lint.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,12 @@ fn check_item(i: @ast::item, cx: ty::ctxt) {
498498
// not traverse into subitems, since that is handled by the outer
499499
// lint visitor.
500500
fn item_stopping_visitor<E>(v: visit::vt<E>) -> visit::vt<E> {
501-
visit::mk_vt(@visit::Visitor {visit_item: |_i, _e, _v| { },.. **v})
501+
visit::mk_vt(@visit::Visitor {visit_item: |_i, _e, _v| { },
502+
.. **(ty_stopping_visitor(v))})
503+
}
504+
505+
fn ty_stopping_visitor<E>(v: visit::vt<E>) -> visit::vt<E> {
506+
visit::mk_vt(@visit::Visitor {visit_ty: |_t, _e, _v| { },.. **v})
502507
}
503508

504509
fn check_item_while_true(cx: ty::ctxt, it: @ast::item) {

src/librustc/middle/typeck/astconv.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
use core::prelude::*;
5656

57+
use middle::const_eval;
5758
use middle::ty::{arg, field, substs};
5859
use middle::ty::{ty_param_substs_and_ty};
5960
use middle::ty;
@@ -412,9 +413,29 @@ pub fn ast_ty_to_ty<AC:AstConv, RS:region_scope + Copy + Durable>(
412413
}
413414
}
414415
}
415-
ast::ty_fixed_length_vec(a_mt, u) => {
416-
ty::mk_evec(tcx, ast_mt_to_mt(self, rscope, a_mt),
417-
ty::vstore_fixed(u))
416+
ast::ty_fixed_length_vec(a_mt, e) => {
417+
match const_eval::eval_const_expr_partial(tcx, e) {
418+
Ok(ref r) => {
419+
match *r {
420+
const_eval::const_int(i) =>
421+
ty::mk_evec(tcx, ast_mt_to_mt(self, rscope, a_mt),
422+
ty::vstore_fixed(i as uint)),
423+
const_eval::const_uint(i) =>
424+
ty::mk_evec(tcx, ast_mt_to_mt(self, rscope, a_mt),
425+
ty::vstore_fixed(i as uint)),
426+
_ => {
427+
tcx.sess.span_fatal(
428+
ast_ty.span, ~"expected constant expr for vector length");
429+
}
430+
}
431+
}
432+
Err(ref r) => {
433+
tcx.sess.span_fatal(
434+
ast_ty.span,
435+
fmt!("expected constant expr for vector length: %s",
436+
*r));
437+
}
438+
}
418439
}
419440
ast::ty_infer => {
420441
// ty_infer should only appear as the type of arguments or return

src/librustc/middle/typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2169,7 +2169,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
21692169
}
21702170
ast::expr_repeat(element, count_expr, mutbl) => {
21712171
let count = ty::eval_repeat_count(tcx, count_expr);
2172-
fcx.write_ty(count_expr.id, ty::mk_uint(tcx));
2172+
check_expr_with_hint(fcx, count_expr, ty::mk_uint(tcx));
21732173
let tt = ast_expr_vstore_to_vstore(fcx, ev, count, vst);
21742174
let t: ty::t = fcx.infcx().next_ty_var();
21752175
bot |= check_expr_has_type(fcx, element, t);
@@ -2537,7 +2537,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
25372537
}
25382538
ast::expr_repeat(element, count_expr, mutbl) => {
25392539
let count = ty::eval_repeat_count(tcx, count_expr);
2540-
fcx.write_ty(count_expr.id, ty::mk_uint(tcx));
2540+
check_expr_with_hint(fcx, count_expr, ty::mk_uint(tcx));
25412541
let t: ty::t = fcx.infcx().next_ty_var();
25422542
bot |= check_expr_has_type(fcx, element, t);
25432543
let t = ty::mk_evec(tcx, ty::mt {ty: t, mutbl: mutbl},

src/libsyntax/ast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ pub enum vstore {
386386
#[auto_decode]
387387
#[deriving_eq]
388388
pub enum expr_vstore {
389-
// FIXME (#3469): Change uint to @expr (actually only constant exprs)
390389
expr_vstore_fixed(Option<uint>), // [1,2,3,4]
391390
expr_vstore_uniq, // ~[1,2,3,4]
392391
expr_vstore_box, // @[1,2,3,4]
@@ -916,7 +915,7 @@ pub enum ty_ {
916915
ty_box(mt),
917916
ty_uniq(mt),
918917
ty_vec(mt),
919-
ty_fixed_length_vec(mt, uint),
918+
ty_fixed_length_vec(mt, @expr),
920919
ty_ptr(mt),
921920
ty_rptr(Option<@Lifetime>, mt),
922921
ty_closure(@TyClosure),

src/libsyntax/fold.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,10 @@ pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ {
622622
}
623623
ty_tup(ref tys) => ty_tup(tys.map(|ty| fld.fold_ty(*ty))),
624624
ty_path(path, id) => ty_path(fld.fold_path(path), fld.new_id(id)),
625-
ty_fixed_length_vec(ref mt, vs) => {
625+
ty_fixed_length_vec(ref mt, e) => {
626626
ty_fixed_length_vec(
627627
fold_mt(mt, fld),
628-
vs
628+
fld.fold_expr(e)
629629
)
630630
}
631631
ty_mac(ref mac) => ty_mac(fold_mac(*mac))

src/libsyntax/parse/parser.rs

+4-17
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ pub impl Parser {
642642
self.obsolete(*self.last_span, ObsoleteMutVector);
643643
}
644644

645-
// Parse the `* 3` in `[ int * 3 ]`
645+
// Parse the `* e` in `[ int * e ]`
646+
// where `e` is a const expression
646647
let t = match self.maybe_parse_fixed_vstore_with_star() {
647648
None => ty_vec(mt),
648649
Some(suffix) => ty_fixed_length_vec(mt, suffix)
@@ -814,23 +815,9 @@ pub impl Parser {
814815
})
815816
}
816817

817-
fn maybe_parse_fixed_vstore_with_star(&self) -> Option<uint> {
818+
fn maybe_parse_fixed_vstore_with_star(&self) -> Option<@ast::expr> {
818819
if self.eat(&token::BINOP(token::STAR)) {
819-
match *self.token {
820-
token::LIT_INT_UNSUFFIXED(i) if i >= 0i64 => {
821-
self.bump();
822-
Some(i as uint)
823-
}
824-
_ => {
825-
self.fatal(
826-
fmt!(
827-
"expected integral vector length \
828-
but found `%s`",
829-
token_to_str(self.reader, &copy *self.token)
830-
)
831-
);
832-
}
833-
}
820+
Some(self.parse_expr())
834821
} else {
835822
None
836823
}

src/libsyntax/print/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ pub fn print_type_ex(s: @ps, &&ty: @ast::Ty, print_colons: bool) {
425425
}
426426
print_type(s, mt.ty);
427427
word(s.s, ~" * ");
428-
word(s.s, fmt!("%u", v));
428+
print_expr(s, v);
429429
word(s.s, ~"]");
430430
}
431431
ast::ty_mac(_) => {

src/libsyntax/visit.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ pub fn visit_ty<E>(t: @Ty, e: E, v: vt<E>) {
246246
(v.visit_ty)(f.decl.output, e, v);
247247
},
248248
ty_path(p, _) => visit_path(p, e, v),
249-
ty_fixed_length_vec(ref mt, _) => (v.visit_ty)(mt.ty, e, v),
249+
ty_fixed_length_vec(ref mt, ex) => {
250+
(v.visit_ty)(mt.ty, e, v);
251+
(v.visit_expr)(ex, e, v);
252+
},
250253
ty_nil | ty_bot | ty_mac(_) | ty_infer => ()
251254
}
252255
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012-2013 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+
// Check that non-constant exprs do fail as count in fixed length vec type
12+
13+
fn main() {
14+
fn bar(n: int) {
15+
let _x: [int * n]; //~ ERROR expected constant expr for vector length: Non-constant path in constant expr
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012-2013 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+
// Check that non constant exprs fail for vector repeat syntax
12+
13+
fn main() {
14+
fn bar(n: int) {
15+
let _x = [0, ..n]; //~ ERROR expected constant integer for repeat count but found variable
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012-2013 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+
// Check that constant expressions can be used for declaring the
12+
// type of a fixed length vector.
13+
14+
fn main() {
15+
16+
const FOO: int = 2;
17+
let _v: [int * FOO*3];
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2012-2013 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+
// Check that constant expressions can be used in vec repeat syntax.
12+
13+
fn main() {
14+
15+
const FOO: int = 2;
16+
let _v = [0, ..FOO*3*2/2];
17+
18+
}

0 commit comments

Comments
 (0)