diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-13 23:16:30 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-14 11:12:06 +1000 |
| commit | f93a974558bf09bee475427158150ba6e008cdd8 (patch) | |
| tree | b33c58f8819e4a85af750404e40b453aac5f2b3e | |
| parent | b417bc8511a0a38e73312cee8086ed04eeb21b75 (diff) | |
| download | rust-f93a974558bf09bee475427158150ba6e008cdd8.tar.gz rust-f93a974558bf09bee475427158150ba6e008cdd8.zip | |
syntax: correct the Rand::rand call to select enum variants in #[deriving(Rand)].
Previously, this was not a global call, and so when `#[deriving(Rand)]` was in any module other than the top-level one, it failed (unless there was a `use std;` in scope). Also, fix a minor inconsistency between uints and u32s for this piece of code.
| -rw-r--r-- | src/libsyntax/ext/deriving/rand.rs | 9 | ||||
| -rw-r--r-- | src/test/run-pass/deriving-global.rs | 39 |
2 files changed, 44 insertions, 4 deletions
diff --git a/src/libsyntax/ext/deriving/rand.rs b/src/libsyntax/ext/deriving/rand.rs index ab5ac6d7847..79aa3469f74 100644 --- a/src/libsyntax/ext/deriving/rand.rs +++ b/src/libsyntax/ext/deriving/rand.rs @@ -78,19 +78,20 @@ fn rand_substructure(cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr { let variant_count = cx.expr_uint(span, variants.len()); - // need to specify the uint-ness of the random number - let u32_ty = cx.ty_ident(span, cx.ident_of("uint")); + // need to specify the u32-ness of the random number + let u32_ty = cx.ty_ident(span, cx.ident_of("u32")); let r_ty = cx.ty_ident(span, cx.ident_of("R")); - let rand_name = cx.path_all(span, false, copy rand_ident, None, ~[ u32_ty, r_ty ]); + let rand_name = cx.path_all(span, true, copy rand_ident, None, ~[ u32_ty, r_ty ]); let rand_name = cx.expr_path(rand_name); + // ::std::rand::Rand::rand::<u32>(rng) let rv_call = cx.expr_call(span, rand_name, ~[ rng[0].duplicate(cx) ]); // rand() % variants.len() let rand_variant = cx.expr_binary(span, ast::rem, - rv_call, variant_count); + rv_call, variant_count); let mut arms = do variants.mapi |i, id_sum| { let i_expr = cx.expr_uint(span, i); diff --git a/src/test/run-pass/deriving-global.rs b/src/test/run-pass/deriving-global.rs new file mode 100644 index 00000000000..fce6d17c5f0 --- /dev/null +++ b/src/test/run-pass/deriving-global.rs @@ -0,0 +1,39 @@ +// Copyright 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. + +extern mod extra; // {En,De}codable +mod submod { + // if any of these are implemented without global calls for any + // function calls, then being in a submodule will (correctly) + // cause errors about unrecognised module `std` (or `extra`) + #[deriving(Eq, Ord, TotalEq, TotalOrd, + IterBytes, + Clone, DeepClone, + ToStr, Rand, + Encodable, Decodable)] + enum A { A1(uint), A2(int) } + + #[deriving(Eq, Ord, TotalEq, TotalOrd, + IterBytes, + Clone, DeepClone, + ToStr, Rand, + Encodable, Decodable)] + struct B { x: uint, y: int } + + #[deriving(Eq, Ord, TotalEq, TotalOrd, + IterBytes, + Clone, DeepClone, + ToStr, Rand, + Encodable, Decodable)] + struct C(uint, int); + +} + +fn main() {} |
