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

+33-29
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

-5
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

-5
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

-2
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

+5-6
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

-5
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

-5
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

-5
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

+2-3
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

+1
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;

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

-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ use std::list::Nil;
2525
use std::list;
2626
use syntax::ast::{m_const, purity, ret_style};
2727

28-
pub fn macros() {
29-
// FIXME(#3114): Macro import/export.
30-
include!("macros.rs");
31-
}
3228

3329
pub enum Sub = CombineFields; // "subtype", "subregion" etc
3430

src/librustc/rustc.rc

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use back_ = back;
4949

5050
pub mod middle {
5151
pub mod trans {
52+
pub mod macros;
5253
pub mod inline;
5354
pub mod monomorphize;
5455
pub mod controlflow;

src/librustdoc/extract.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn nmoddoc_from_mod(
161161
ast::foreign_item_const(*) => {} // XXX: Not implemented.
162162
}
163163
}
164-
doc:: NmodDoc {
164+
doc::NmodDoc {
165165
item: itemdoc,
166166
fns: fns,
167167
index: None

src/libsyntax/ast.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,37 @@ macro_rules! interner_key (
2929
(-3 as uint, 0u)))
3030
)
3131

32+
// an identifier contains an index into the interner
33+
// table and a SyntaxContext to track renaming and
34+
// macro expansion per Flatt et al., "Macros
35+
// That Work Together"
3236
#[deriving_eq]
33-
pub struct ident { repr: uint }
37+
pub struct ident { repr: Name }
38+
39+
// a SyntaxContext represents a chain of macro-expandings
40+
// and renamings. Each macro expansion corresponds to
41+
// a fresh uint
42+
#[deriving_eq]
43+
pub enum SyntaxContext {
44+
MT,
45+
Mark (Mrk,~SyntaxContext),
46+
Rename (~ident,Name,~SyntaxContext)
47+
}
48+
49+
/*
50+
// ** this is going to have to apply to paths, not to idents.
51+
// Returns true if these two identifiers access the same
52+
// local binding or top-level binding... that's what it
53+
// should do. For now, it just compares the names.
54+
pub fn free_ident_eq (a : ident, b: ident) -> bool{
55+
a.repr == b.repr
56+
}
57+
*/
58+
// a name represents a string, interned
59+
type Name = uint;
60+
// a mark represents a unique id associated
61+
// with a macro expansion
62+
type Mrk = uint;
3463

3564
pub impl<S:Encoder> Encodable<S> for ident {
3665
fn encode(&self, s: &S) {
@@ -1230,6 +1259,7 @@ pub enum item_ {
12301259
Option<@trait_ref>, // (optional) trait this impl implements
12311260
@Ty, // self
12321261
~[@method]),
1262+
// a macro invocation (which includes macro definition)
12331263
item_mac(mac),
12341264
}
12351265

src/libsyntax/ext/auto_encode.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,8 @@ mod test {
11731173
CallToEmitEnumVariantArg(uint),
11741174
CallToEmitUint(uint),
11751175
CallToEmitNil,
1176+
CallToEmitStruct(~str,uint),
1177+
CallToEmitField(~str,uint),
11761178
// all of the ones I was too lazy to handle:
11771179
CallToOther
11781180
}
@@ -1251,11 +1253,11 @@ mod test {
12511253
fn emit_rec(&self, f: fn()) {
12521254
self.add_unknown_to_log(); f();
12531255
}
1254-
fn emit_struct(&self, _name: &str, +_len: uint, f: fn()) {
1255-
self.add_unknown_to_log(); f();
1256+
fn emit_struct(&self, name: &str, +len: uint, f: fn()) {
1257+
self.add_to_log(CallToEmitStruct (name.to_str(),len)); f();
12561258
}
1257-
fn emit_field(&self, _name: &str, +_idx: uint, f: fn()) {
1258-
self.add_unknown_to_log(); f();
1259+
fn emit_field(&self, name: &str, +idx: uint, f: fn()) {
1260+
self.add_to_log(CallToEmitField (name.to_str(),idx)); f();
12591261
}
12601262
12611263
fn emit_tup(&self, +_len: uint, f: fn()) {
@@ -1267,23 +1269,12 @@ mod test {
12671269
}
12681270
12691271
1270-
#[auto_decode]
1271-
#[auto_encode]
1272-
struct Node {id: uint}
1273-
12741272
fn to_call_log (val: Encodable<TestEncoder>) -> ~[call] {
12751273
let mut te = TestEncoder {call_log: @mut ~[]};
12761274
val.encode(&te);
12771275
copy *te.call_log
12781276
}
1279-
/*
1280-
#[test] fn encode_test () {
1281-
check_equal (to_call_log(Node{id:34}
1282-
as Encodable::<std::json::Encoder>),
1283-
~[CallToEnum (~"Node"),
1284-
CallToEnumVariant]);
1285-
}
1286-
*/
1277+
12871278
#[auto_encode]
12881279
enum Written {
12891280
Book(uint,uint),
@@ -1300,4 +1291,17 @@ mod test {
13001291
CallToEmitEnumVariantArg (1),
13011292
CallToEmitUint (44)]);
13021293
}
1294+
1295+
pub enum BPos = uint;
1296+
1297+
#[auto_encode]
1298+
pub struct HasPos { pos : BPos }
1299+
1300+
#[test] fn encode_newtype_test () {
1301+
check_equal (to_call_log (HasPos {pos:BPos(48)}
1302+
as Encodable::<TestEncoder>),
1303+
~[CallToEmitStruct(~"HasPos",1),
1304+
CallToEmitField(~"pos",0),
1305+
CallToEmitUint(48)]);
1306+
}
13031307
}

0 commit comments

Comments
 (0)