Skip to content

Rollup of 3 pull requests #20121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3345,11 +3345,38 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
actual)
},
expr_t, None);
suggest_field_names(expr_t, field, tcx);
}

fcx.write_error(expr.id);
}

fn suggest_field_names<'tcx>(t : ty::t,
field : &ast::SpannedIdent,
tcx : &ty::ctxt<'tcx>) {
let id = ty::ty_to_def_id(t).unwrap();
let ident = token::get_ident(field.node);
let name = ident.get();
// only find fits with at least one matching letter
let mut best_dist = name.len();
let mut best = vec![];
let fields = ty::lookup_struct_fields(tcx, id);
for elem in fields.iter() {
let n = elem.name.as_str();
let dist = n.lev_distance(name);
if dist < best_dist {
best = vec![n];
best_dist = dist;
} else if dist == best_dist {
best.push(n);
}
}
for n in best.iter() {
tcx.sess.span_help(field.span,
format!("did you mean `{}`?", n).as_slice());
}
}

// Check tuple index expressions
fn check_tup_field(fcx: &FnCtxt,
expr: &ast::Expr,
Expand Down Expand Up @@ -3445,6 +3472,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
},
struct_ty,
None);
// FIXME: suggest_field_names also displays already defined fields
suggest_field_names(struct_ty, &field.ident, tcx);
error_happened = true;
}
Some((_, true)) => {
Expand Down
96 changes: 95 additions & 1 deletion src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,47 @@ pub trait MacResult {
}
}

/// Single type implementing MacResult with Option fields for all the types
/// MacResult can return, and a Default impl that fills in None.
pub struct MacGeneral {
expr: Option<P<ast::Expr>>,
pat: Option<P<ast::Pat>>,
items: Option<SmallVector<P<ast::Item>>>,
methods: Option<SmallVector<P<ast::Method>>>,
def: Option<MacroDef>
}
impl MacGeneral {
pub fn new(expr: Option<P<ast::Expr>>,
pat: Option<P<ast::Pat>>,
items: Option<SmallVector<P<ast::Items>>>,
methods: Option<SmallVector<P<ast::Method>>>,
def: Option<MacroDef>)
-> Box<MacResult+'static> {
box MacGeneral { expr: expr, pat: pat, items: items,
methods: methods, def: def } as Box<MacResult+'static>
}
pub fn Default() -> Box<MacResult+'static> {
box MacGeneral { expr: None, pat: None, items: None
methods: None, def: None} as Box<MacResult+'static>
}
}
impl MacResult for MacGeneral {
fn make_expr(self: Box<MacGeneral>) -> Option<P<ast::Expr>> {
self.expr
}
fn make_pat(self: Box<MacGeneral>) -> Option<P<ast::Pat>> {
self.pat
}
fn make_items(self: Box<MacGeneral>) -> Option<SmallVector<P<ast::Item>>> {
self.items
}
fn make_methods(self: Box<Self>) -> Option<SmallVector<P<ast::Method>>> {
self.methods
}
fn make_def(&mut self) -> Option<MacroDef> {
self.def
}
}
/// A convenience type for macros that return a single expression.
pub struct MacExpr {
e: P<ast::Expr>
Expand Down Expand Up @@ -204,7 +245,7 @@ impl MacResult for MacPat {
Some(self.p)
}
}
/// A type for macros that return multiple items.
/// A convenience type for macros that return multiple items.
pub struct MacItems {
items: SmallVector<P<ast::Item>>
}
Expand All @@ -221,6 +262,59 @@ impl MacResult for MacItems {
}
}

/// A convenience type for macros that return a single statement
pub struct MacStmt {
stmt: P<ast::Stmt>,
}

impl MacStmt {
pub fn new(stmt: P<ast::Stmt>) -> MacStmt {
MacStmt{ stmt: stmt }
}
}

impl MacResult for MacStmt {
fn make_stmt(self: Box<MacStmt>) -> Option<P<ast::Stmt>> {
Some(self.stmt)
}
}


/// A convenience type for macros that return a macro def
pub struct MacDef {
def: Option<MacroDef>
}

impl MacDef {
pub fn new(def: MacroDef) -> MacDef {
MacDef{ def: Some(def) }
}
}

impl MacResult for MacDef {
fn make_def(&mut self) -> Option<MacroDef> {
Some(self.def.take().expect("empty MacDef"))
}
}

/// A convenience type for macros that return methods
pub struct MacMethods {
methods: SmallVector<P<ast::Method>>
}

impl MacMethods {
pub fn new(methods: SmallVector<P<ast::Method>>) -> MacMethods {
MacMethods{ methods: methods }
}
}

impl MacResult for MacMethods {
fn make_methods(self: Box<MacMethods>) -> Option<SmallVector<P<ast::Method>>> {
Some(self.methods)
}
}


/// Fill-in macro expansion result, to allow compilation to continue
/// after hitting errors.
#[deriving(Copy)]
Expand Down
18 changes: 5 additions & 13 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use ast::{Ident, TtDelimited, TtSequence, TtToken};
use ast;
use codemap::{Span, DUMMY_SP};
use ext::base::{ExtCtxt, MacResult, MacroDef};
use ext::base::{ExtCtxt, MacDef, MacResult, MacroDef};
use ext::base::{NormalTT, TTMacroExpander};
use ext::tt::macro_parser::{Success, Error, Failure};
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
Expand Down Expand Up @@ -129,14 +129,6 @@ impl TTMacroExpander for MacroRulesMacroExpander {
}
}

struct MacroRulesDefiner {
def: Option<MacroDef>
}
impl MacResult for MacroRulesDefiner {
fn make_def(&mut self) -> Option<MacroDef> {
Some(self.def.take().expect("empty MacroRulesDefiner"))
}
}

/// Given `lhses` and `rhses`, this is the new macro we create
fn generic_extension<'cx>(cx: &'cx ExtCtxt,
Expand Down Expand Up @@ -279,10 +271,10 @@ pub fn add_new_extension<'cx>(cx: &'cx mut ExtCtxt,
rhses: rhses,
};

box MacroRulesDefiner {
def: Some(MacroDef {
box MacDef::new(
MacroDef {
name: token::get_ident(name).to_string(),
ext: NormalTT(exp, Some(sp))
})
} as Box<MacResult+'cx>
}
) as Box<MacResult+'cx>
}