You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the course of answering this StackOverflow question I was puzzling out how to enclose the body of a macro definition when I struck upon the idea of using a self-executing anonymous closure. This worked! (Although there was some weirdness with rustc throwing errors due to being unable to infer the type of the macro invocations, forcing me to include an explicit return statement. But that's beside the point.)
Afterward while looking through Rust's test suite I checked out the macro-by-example tests and saw that the bodies of macros could simply be enclosed in braces (seen here, in the definition for #lambda). Converting my code from the anonymous self-executing closure to use the braces syntax causes rustc to hang indefinitely, consuming 100% CPU.
I think this has been fixed: the code in question successfully expands, and, after fixing errors caused by language and library changes, compiles, also. The functioning code is below:
// Old comment:
// This program hangs in the compilation phase.
// The only difference is that the closure in #macro is replaced with a block
use std;
import rand;
import io::println;
iface is_even { fn is_even() -> bool; }
impl of is_even for int { fn is_even() -> bool { self % 2 == 0 } }
fn main() {
let rng = rand::rng();
let gen_even = {|| (rng.next() as int) * 2};
let gen_float = {|| rng.gen_float()};
fn assert_even<T: is_even>(num_gen: fn() -> T) -> bool {
let num = num_gen();
let prop_holds = num.is_even();
if !prop_holds {
println(#fmt("Failure: %? is not even", num));
}
ret prop_holds;
}
fn assert_divisible(num_gen1: fn() -> float,
num_gen2: fn() -> float) -> bool {
let dividend = num_gen1(),
divisor = num_gen2();
let prop_holds = dividend / divisor == 0f;
if !prop_holds {
println(#fmt("Failure: %? are not divisible", (dividend, divisor)));
}
ret prop_holds;
}
// Change closure to block
#macro[[#for_all[prop, [gen, ...]], {
let mut passed_tests = 0;
let mut prop_holds = true;
while passed_tests < 100 && prop_holds {
prop_holds = prop(gen, ...);
if prop_holds { passed_tests += 1; }
}
println(#fmt("Tests passed: %d", passed_tests));
}]]; // Close block
#for_all[assert_even, [gen_even]];
#for_all[assert_divisible, [gen_float, gen_float]];
}
Activity
paulstansifer commentedon Jul 31, 2012
I think this has been fixed: the code in question successfully expands, and, after fixing errors caused by language and library changes, compiles, also. The functioning code is below:
Update issue templates (rust-lang#1885)
Merge pull request rust-lang#1885 from 197g/maskmove-is-non-temporal
Merge rust-lang#1885
Auto merge of rust-lang#1885 - DrMeepster:global_allocator, r=RalfJung