Skip to content

Commit 5efaa4f

Browse files
committed
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.
1 parent a198aad commit 5efaa4f

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/libsyntax/ext/deriving/rand.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,20 @@ fn rand_substructure(cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr {
7878

7979
let variant_count = cx.expr_uint(span, variants.len());
8080

81-
// need to specify the uint-ness of the random number
82-
let u32_ty = cx.ty_ident(span, cx.ident_of("uint"));
81+
// need to specify the u32-ness of the random number
82+
let u32_ty = cx.ty_ident(span, cx.ident_of("u32"));
8383
let r_ty = cx.ty_ident(span, cx.ident_of("R"));
84-
let rand_name = cx.path_all(span, false, copy rand_ident, None, ~[ u32_ty, r_ty ]);
84+
let rand_name = cx.path_all(span, true, copy rand_ident, None, ~[ u32_ty, r_ty ]);
8585
let rand_name = cx.expr_path(rand_name);
8686

87+
// ::std::rand::Rand::rand::<u32>(rng)
8788
let rv_call = cx.expr_call(span,
8889
rand_name,
8990
~[ rng[0].duplicate(cx) ]);
9091

9192
// rand() % variants.len()
9293
let rand_variant = cx.expr_binary(span, ast::rem,
93-
rv_call, variant_count);
94+
rv_call, variant_count);
9495

9596
let mut arms = do variants.mapi |i, id_sum| {
9697
let i_expr = cx.expr_uint(span, i);

src/test/run-pass/deriving-global.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 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+
extern mod extra; // {En,De}codable
12+
mod submod {
13+
// if any of these are implemented without global calls for any
14+
// function calls, then being in a submodule will (correctly)
15+
// cause errors about unrecognised module `std` (or `extra`)
16+
#[deriving(Eq, Ord, TotalEq, TotalOrd,
17+
IterBytes,
18+
Clone, DeepClone,
19+
ToStr, Rand,
20+
Encodable, Decodable)]
21+
enum A { A1(uint), A2(int) }
22+
23+
#[deriving(Eq, Ord, TotalEq, TotalOrd,
24+
IterBytes,
25+
Clone, DeepClone,
26+
ToStr, Rand,
27+
Encodable, Decodable)]
28+
struct B { x: uint, y: int }
29+
30+
#[deriving(Eq, Ord, TotalEq, TotalOrd,
31+
IterBytes,
32+
Clone, DeepClone,
33+
ToStr, Rand,
34+
Encodable, Decodable)]
35+
struct C(uint, int);
36+
37+
}
38+
39+
fn main() {}

0 commit comments

Comments
 (0)