Skip to content

Commit 99a902c

Browse files
committed
auto merge of #5120 : jbclements/rust/macros-have-scope, r=pcwalton
r? After this patch, macros declared in a module, function, or block can only be used inside of that module, function or block, with the exception of modules declared with the #[macro_escape] attribute; these modules allow macros to escape, and can be used as a limited macro export mechanism. This pull request also includes miscellaneous comments, lots of new test cases, a few renamings, and a few as-yet-unused data definitions for hygiene.
2 parents 28b50a4 + 6aefaf2 commit 99a902c

File tree

21 files changed

+647
-197
lines changed

21 files changed

+647
-197
lines changed

src/libcore/hash.rs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -186,42 +186,46 @@ fn SipState(key0: u64, key1: u64) -> SipState {
186186
state
187187
}
188188

189+
// sadly, these macro definitions can't appear later,
190+
// because they're needed in the following defs;
191+
// this design could be improved.
192+
193+
macro_rules! u8to64_le (
194+
($buf:expr, $i:expr) =>
195+
($buf[0+$i] as u64 |
196+
$buf[1+$i] as u64 << 8 |
197+
$buf[2+$i] as u64 << 16 |
198+
$buf[3+$i] as u64 << 24 |
199+
$buf[4+$i] as u64 << 32 |
200+
$buf[5+$i] as u64 << 40 |
201+
$buf[6+$i] as u64 << 48 |
202+
$buf[7+$i] as u64 << 56)
203+
)
204+
205+
macro_rules! rotl (
206+
($x:expr, $b:expr) =>
207+
(($x << $b) | ($x >> (64 - $b)))
208+
)
209+
210+
macro_rules! compress (
211+
($v0:expr, $v1:expr, $v2:expr, $v3:expr) =>
212+
({
213+
$v0 += $v1; $v1 = rotl!($v1, 13); $v1 ^= $v0;
214+
$v0 = rotl!($v0, 32);
215+
$v2 += $v3; $v3 = rotl!($v3, 16); $v3 ^= $v2;
216+
$v0 += $v3; $v3 = rotl!($v3, 21); $v3 ^= $v0;
217+
$v2 += $v1; $v1 = rotl!($v1, 17); $v1 ^= $v2;
218+
$v2 = rotl!($v2, 32);
219+
})
220+
)
221+
189222

190223
impl io::Writer for SipState {
191224

192225
// Methods for io::writer
193226
#[inline(always)]
194227
fn write(&self, msg: &[const u8]) {
195228

196-
macro_rules! u8to64_le (
197-
($buf:expr, $i:expr) =>
198-
($buf[0+$i] as u64 |
199-
$buf[1+$i] as u64 << 8 |
200-
$buf[2+$i] as u64 << 16 |
201-
$buf[3+$i] as u64 << 24 |
202-
$buf[4+$i] as u64 << 32 |
203-
$buf[5+$i] as u64 << 40 |
204-
$buf[6+$i] as u64 << 48 |
205-
$buf[7+$i] as u64 << 56)
206-
);
207-
208-
macro_rules! rotl (
209-
($x:expr, $b:expr) =>
210-
(($x << $b) | ($x >> (64 - $b)))
211-
);
212-
213-
macro_rules! compress (
214-
($v0:expr, $v1:expr, $v2:expr, $v3:expr) =>
215-
({
216-
$v0 += $v1; $v1 = rotl!($v1, 13); $v1 ^= $v0;
217-
$v0 = rotl!($v0, 32);
218-
$v2 += $v3; $v3 = rotl!($v3, 16); $v3 ^= $v2;
219-
$v0 += $v3; $v3 = rotl!($v3, 21); $v3 ^= $v0;
220-
$v2 += $v1; $v1 = rotl!($v1, 17); $v1 ^= $v2;
221-
$v2 = rotl!($v2, 32);
222-
})
223-
);
224-
225229
let length = msg.len();
226230
self.length += length;
227231

src/librustc/middle/trans/_match.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ use syntax::ast_util;
172172
use syntax::codemap::span;
173173
use syntax::print::pprust::pat_to_str;
174174

175-
pub fn macros() {
176-
// FIXME(#3114): Macro import/export.
177-
include!("macros.rs");
178-
}
179-
180175
// An option identifying a literal: either a unit-like struct or an
181176
// expression.
182177
pub enum Lit {

src/librustc/middle/trans/controlflow.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ use middle::trans::datum::*;
1818

1919
use core::str;
2020

21-
pub fn macros() {
22-
// FIXME(#3114): Macro import/export.
23-
include!("macros.rs");
24-
}
25-
2621
pub fn trans_block(bcx: block, b: &ast::blk, dest: expr::Dest) -> block {
2722
let _icx = bcx.insn_ctxt("trans_block");
2823
let mut bcx = bcx;

src/librustc/middle/trans/expr.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ use syntax::codemap::spanned;
149149
// These are passed around by the code generating functions to track the
150150
// destination of a computation's value.
151151

152-
fn macros() { include!("macros.rs"); } // FIXME(#3114): Macro import/export.
153-
154152
pub enum Dest {
155153
SaveIn(ValueRef),
156154
Ignore,

src/librustc/middle/trans/macros.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
{
11+
#[macro_escape];
1212

1313
macro_rules! unpack_datum(
1414
($bcx: ident, $inp: expr) => (
@@ -18,7 +18,7 @@ macro_rules! unpack_datum(
1818
db.datum
1919
}
2020
)
21-
);
21+
)
2222

2323
macro_rules! unpack_result(
2424
($bcx: ident, $inp: expr) => (
@@ -28,7 +28,7 @@ macro_rules! unpack_result(
2828
db.val
2929
}
3030
)
31-
);
31+
)
3232

3333
macro_rules! trace_span(
3434
($bcx: ident, $sp: expr, $str: expr) => (
@@ -39,7 +39,7 @@ macro_rules! trace_span(
3939
}
4040
}
4141
)
42-
);
42+
)
4343

4444
macro_rules! trace(
4545
($bcx: ident, $str: expr) => (
@@ -50,6 +50,5 @@ macro_rules! trace(
5050
}
5151
}
5252
)
53-
);
53+
)
5454

55-
}

src/librustc/middle/trans/meth.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ use syntax::ast_util::local_def;
3939
use syntax::print::pprust::expr_to_str;
4040
use syntax::{ast, ast_map};
4141

42-
pub fn macros() {
43-
// FIXME(#3114): Macro import/export.
44-
include!("macros.rs");
45-
}
46-
4742
/**
4843
The main "translation" pass for methods. Generates code
4944
for non-monomorphized methods only. Other methods will

src/librustc/middle/typeck/infer/combine.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ use syntax::ast::{Onceness, purity, ret_style};
7272
use syntax::ast;
7373
use syntax::codemap::span;
7474

75-
pub fn macros() {
76-
// FIXME(#3114): Macro import/export.
77-
include!("macros.rs");
78-
}
79-
8075
pub trait Combine {
8176
fn infcx(&self) -> @mut InferCtxt;
8277
fn tag(&self) -> ~str;

src/librustc/middle/typeck/infer/lub.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ use std::list;
2424
use syntax::ast::{Many, Once, extern_fn, m_const, impure_fn, noreturn};
2525
use syntax::ast::{pure_fn, ret_style, return_val, unsafe_fn};
2626

27-
pub fn macros() {
28-
// FIXME(#3114): Macro import/export.
29-
include!("macros.rs");
30-
}
31-
3227
pub enum Lub = CombineFields; // least-upper-bound: common supertype
3328

3429
pub impl Lub {

src/librustc/middle/typeck/infer/macros.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
{
11+
#[macro_escape];
1212

1313
macro_rules! if_ok(
1414
($inp: expr) => (
@@ -17,6 +17,5 @@ macro_rules! if_ok(
1717
Err(e) => { return Err(e); }
1818
}
1919
)
20-
);
20+
)
2121

22-
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ use syntax::codemap;
287287
use syntax::ast_util;
288288
use syntax::codemap::span;
289289

290+
pub mod macros;
290291
pub mod combine;
291292
pub mod glb;
292293
pub mod lattice;

0 commit comments

Comments
 (0)