Skip to content

Commit 8602a7d

Browse files
committed
1 parent 9fb11fe commit 8602a7d

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ pub fn ast_ty_to_ty<'tcx>(this: &AstConv<'tcx>,
16631663
this.ty_infer(None, None, None, ast_ty.span)
16641664
}
16651665
ast::TyMac(_) => {
1666-
tcx.sess.span_bug(m.span, "unexpanded type macro found conversion")
1666+
tcx.sess.span_bug(ast_ty.span, "unexpanded type macro found conversion")
16671667
}
16681668
};
16691669

src/libsyntax/ext/base.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,19 +416,22 @@ impl DummyResult {
416416

417417
pub fn raw_ty(sp: Span) -> P<ast::Ty> {
418418
P(ast::Ty {
419-
id: ast:DUMMY_NODE_ID,
419+
id: ast::DUMMY_NODE_ID,
420420
node: ast::TyInfer,
421421
span: sp
422422
})
423+
}
423424
}
424425

425426
impl MacResult for DummyResult {
426427
fn make_expr(self: Box<DummyResult>) -> Option<P<ast::Expr>> {
427428
Some(DummyResult::raw_expr(self.span))
428429
}
430+
429431
fn make_pat(self: Box<DummyResult>) -> Option<P<ast::Pat>> {
430432
Some(P(DummyResult::raw_pat(self.span)))
431433
}
434+
432435
fn make_items(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Item>>> {
433436
// this code needs a comment... why not always just return the Some() ?
434437
if self.expr_only {
@@ -437,13 +440,15 @@ impl MacResult for DummyResult {
437440
Some(SmallVector::zero())
438441
}
439442
}
443+
440444
fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVector<P<ast::ImplItem>>> {
441445
if self.expr_only {
442446
None
443447
} else {
444448
Some(SmallVector::zero())
445449
}
446450
}
451+
447452
fn make_stmts(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Stmt>>> {
448453
Some(SmallVector::one(P(
449454
codemap::respan(self.span,

src/test/run-pass/type-macros.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2015 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+
use std::ops::*;
12+
13+
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
14+
struct Nil; // empty HList
15+
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
16+
struct Cons<H, T: HList>(H, T); // cons cell of HList
17+
18+
// trait to classify valid HLists
19+
trait HList {}
20+
impl HList for Nil {}
21+
impl<H, T: HList> HList for Cons<H, T> {}
22+
23+
// term-level macro for HLists
24+
macro_rules! hlist {
25+
{} => { Nil };
26+
{ $head:expr } => { Cons($head, Nil) };
27+
{ $head:expr, $($tail:expr),* } => { Cons($head, hlist!($($tail),*)) };
28+
}
29+
30+
// type-level macro for HLists
31+
macro_rules! HList {
32+
{} => { Nil };
33+
{ $head:ty } => { Cons<$head, Nil> };
34+
{ $head:ty, $($tail:ty),* } => { Cons<$head, HList!($($tail),*)> };
35+
}
36+
37+
// nil case for HList append
38+
impl<Ys: HList> Add<Ys> for Nil {
39+
type Output = Ys;
40+
41+
fn add(self, rhs: Ys) -> Ys {
42+
rhs
43+
}
44+
}
45+
46+
// cons case for HList append
47+
impl<Rec: HList + Sized, X, Xs: HList, Ys: HList> Add<Ys> for Cons<X, Xs> where
48+
Xs: Add<Ys, Output = Rec>,
49+
{
50+
type Output = Cons<X, Rec>;
51+
52+
fn add(self, rhs: Ys) -> Cons<X, Rec> {
53+
Cons(self.0, self.1 + rhs)
54+
}
55+
}
56+
57+
// type macro Expr allows us to expand the + operator appropriately
58+
macro_rules! Expr {
59+
{ ( $($LHS:tt)+ ) } => { Expr!($($LHS)+) };
60+
{ HList ! [ $($LHS:tt)* ] + $($RHS:tt)+ } => { <Expr!(HList![$($LHS)*]) as Add<Expr!($($RHS)+)>>::Output };
61+
{ $LHS:tt + $($RHS:tt)+ } => { <Expr!($LHS) as Add<Expr!($($RHS)+)>>::Output };
62+
{ $LHS:ty } => { $LHS };
63+
}
64+
65+
// test demonstrating term level `xs + ys` and type level `Expr!(Xs + Ys)`
66+
fn main() {
67+
fn aux<Xs: HList, Ys: HList>(xs: Xs, ys: Ys) -> Expr!(Xs + Ys)
68+
where Xs: Add<Ys> {
69+
xs + ys
70+
}
71+
72+
let xs: HList![&str, bool, Vec<u64>] = hlist!["foo", false, vec![]];
73+
let ys: HList![u64, [u8; 3], ()] = hlist![0, [0, 1, 2], ()];
74+
75+
// demonstrate recursive expansion of Expr!
76+
let zs: Expr!((HList![&str] + HList![bool] + HList![Vec<u64>]) +
77+
(HList![u64] + HList![[u8; 3], ()]) +
78+
HList![])
79+
= aux(xs, ys);
80+
assert_eq!(zs, hlist!["foo", false, vec![], 0, [0, 1, 2], ()])
81+
}
82+

0 commit comments

Comments
 (0)