lib.rs 149.9 KB
Newer Older
1
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

11
#![crate_name = "rustc_resolve"]
12
#![unstable(feature = "rustc_private", issue = "27812")]
13 14
#![crate_type = "dylib"]
#![crate_type = "rlib"]
15
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
A
Alex Crichton 已提交
16
      html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
17
      html_root_url = "https://doc.rust-lang.org/nightly/")]
18
#![cfg_attr(not(stage0), deny(warnings))]
19

T
Fallout  
Tamir Duberstein 已提交
20
#![feature(associated_consts)]
21
#![feature(borrow_state)]
A
Alex Crichton 已提交
22
#![feature(rustc_diagnostic_macros)]
23
#![feature(rustc_private)]
A
Alex Crichton 已提交
24
#![feature(staged_api)]
25

C
corentih 已提交
26 27 28 29
#[macro_use]
extern crate log;
#[macro_use]
extern crate syntax;
30
extern crate arena;
C
corentih 已提交
31 32 33
#[macro_use]
#[no_link]
extern crate rustc_bitflags;
34
extern crate rustc_front;
35 36
extern crate rustc;

S
Steven Fackler 已提交
37 38 39 40 41 42 43 44
use self::PatternBindingMode::*;
use self::Namespace::*;
use self::ResolveResult::*;
use self::FallbackSuggestion::*;
use self::TypeParameters::*;
use self::RibKind::*;
use self::UseLexicalScopeFlag::*;
use self::ModulePrefixResult::*;
45
use self::AssocItemResolveResult::*;
S
Steven Fackler 已提交
46 47 48 49
use self::BareIdentifierPatternResolution::*;
use self::ParentLink::*;
use self::FallbackChecks::*;

50
use rustc::dep_graph::DepNode;
51
use rustc::front::map as hir_map;
52 53
use rustc::session::Session;
use rustc::lint;
54
use rustc::middle::cstore::{CrateStore, DefLike, DlDef};
55
use rustc::middle::def::*;
56
use rustc::middle::def_id::DefId;
57
use rustc::middle::pat_util::pat_bindings;
58 59
use rustc::middle::privacy::*;
use rustc::middle::subst::{ParamSpace, FnSpace, TypeSpace};
60
use rustc::middle::ty::{Freevar, FreevarMap, TraitMap, GlobMap};
61
use rustc::util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
62

P
Patrick Walton 已提交
63
use syntax::ast;
J
Jeffrey Seyfried 已提交
64
use syntax::ast::{CRATE_NODE_ID, Name, NodeId, CrateNum, TyIs, TyI8, TyI16, TyI32, TyI64};
65
use syntax::ast::{TyUs, TyU8, TyU16, TyU32, TyU64, TyF64, TyF32};
66
use syntax::attr::AttrMetaMethods;
67
use syntax::codemap::{self, Span, Pos};
N
Nick Cameron 已提交
68 69
use syntax::errors::DiagnosticBuilder;
use syntax::parse::token::{self, special_names, special_idents};
70
use syntax::util::lev_distance::find_best_match_for_name;
71

72
use rustc_front::intravisit::{self, FnKind, Visitor};
73 74
use rustc_front::hir;
use rustc_front::hir::{Arm, BindByRef, BindByValue, BindingMode, Block};
75
use rustc_front::hir::Crate;
76
use rustc_front::hir::{Expr, ExprAgain, ExprBreak, ExprCall, ExprField};
77 78 79 80 81 82
use rustc_front::hir::{ExprLoop, ExprWhile, ExprMethodCall};
use rustc_front::hir::{ExprPath, ExprStruct, FnDecl};
use rustc_front::hir::{ForeignItemFn, ForeignItemStatic, Generics};
use rustc_front::hir::{ImplItem, Item, ItemConst, ItemEnum, ItemExternCrate};
use rustc_front::hir::{ItemFn, ItemForeignMod, ItemImpl, ItemMod, ItemStatic, ItemDefaultImpl};
use rustc_front::hir::{ItemStruct, ItemTrait, ItemTy, ItemUse};
83
use rustc_front::hir::Local;
84 85
use rustc_front::hir::{Pat, PatEnum, PatIdent, PatLit, PatQPath};
use rustc_front::hir::{PatRange, PatStruct, Path, PrimTy};
86 87
use rustc_front::hir::{TraitRef, Ty, TyBool, TyChar, TyFloat, TyInt};
use rustc_front::hir::{TyRptr, TyStr, TyUint, TyPath, TyPtr};
88
use rustc_front::util::walk_pat;
89

90
use std::collections::{hash_map, HashMap, HashSet};
91
use std::cell::{Cell, RefCell};
92
use std::fmt;
93
use std::mem::replace;
94

J
Jeffrey Seyfried 已提交
95
use resolve_imports::{ImportDirective, ImportResolution};
96

97 98 99 100
// NB: This module needs to be declared first so diagnostics are
// registered before they are used.
pub mod diagnostics;

A
Alex Crichton 已提交
101
mod check_unused;
102
mod build_reduced_graph;
103
mod resolve_imports;
104

105 106 107 108 109 110 111 112 113 114 115
// Perform the callback, not walking deeper if the return is true
macro_rules! execute_callback {
    ($node: expr, $walker: expr) => (
        if let Some(ref callback) = $walker.callback {
            if callback($node, &mut $walker.resolved) {
                return;
            }
        }
    )
}

116 117
enum SuggestionType {
    Macro(String),
118
    Function(token::InternedString),
119 120 121
    NotFound,
}

122
pub enum ResolutionError<'a> {
123
    /// error E0401: can't use type parameters from outer function
124
    TypeParametersFromOuterFunction,
125
    /// error E0402: cannot use an outer type parameter in this context
126
    OuterTypeParameterContext,
127
    /// error E0403: the name is already used for a type parameter in this type parameter list
128
    NameAlreadyUsedInTypeParameterList(Name),
129
    /// error E0404: is not a trait
130
    IsNotATrait(&'a str),
131
    /// error E0405: use of undeclared trait name
132
    UndeclaredTraitName(&'a str),
133
    /// error E0406: undeclared associated type
134
    UndeclaredAssociatedType,
135
    /// error E0407: method is not a member of trait
136
    MethodNotMemberOfTrait(Name, &'a str),
137 138 139 140
    /// error E0437: type is not a member of trait
    TypeNotMemberOfTrait(Name, &'a str),
    /// error E0438: const is not a member of trait
    ConstNotMemberOfTrait(Name, &'a str),
141
    /// error E0408: variable `{}` from pattern #1 is not bound in pattern
142
    VariableNotBoundInPattern(Name, usize),
143
    /// error E0409: variable is bound with different mode in pattern #{} than in pattern #1
144
    VariableBoundWithDifferentMode(Name, usize),
145
    /// error E0410: variable from pattern is not bound in pattern #1
146
    VariableNotBoundInParentPattern(Name, usize),
147
    /// error E0411: use of `Self` outside of an impl or trait
148
    SelfUsedOutsideImplOrTrait,
149
    /// error E0412: use of undeclared
150
    UseOfUndeclared(&'a str, &'a str),
151
    /// error E0413: declaration shadows an enum variant or unit-like struct in scope
152
    DeclarationShadowsEnumVariantOrUnitLikeStruct(Name),
153
    /// error E0414: only irrefutable patterns allowed here
154
    OnlyIrrefutablePatternsAllowedHere(DefId, Name),
155
    /// error E0415: identifier is bound more than once in this parameter list
156
    IdentifierBoundMoreThanOnceInParameterList(&'a str),
157
    /// error E0416: identifier is bound more than once in the same pattern
158
    IdentifierBoundMoreThanOnceInSamePattern(&'a str),
159
    /// error E0417: static variables cannot be referenced in a pattern
160
    StaticVariableReference,
161
    /// error E0418: is not an enum variant, struct or const
162
    NotAnEnumVariantStructOrConst(&'a str),
163
    /// error E0419: unresolved enum variant, struct or const
164
    UnresolvedEnumVariantStructOrConst(&'a str),
165
    /// error E0420: is not an associated const
166
    NotAnAssociatedConst(&'a str),
167
    /// error E0421: unresolved associated const
168
    UnresolvedAssociatedConst(&'a str),
169
    /// error E0422: does not name a struct
170
    DoesNotNameAStruct(&'a str),
171
    /// error E0423: is a struct variant name, but this expression uses it like a function name
172
    StructVariantUsedAsFunction(&'a str),
173
    /// error E0424: `self` is not available in a static method
174
    SelfNotAvailableInStaticMethod,
175
    /// error E0425: unresolved name
176
    UnresolvedName(&'a str, &'a str, UnresolvedNameContext),
177
    /// error E0426: use of undeclared label
178
    UndeclaredLabel(&'a str),
179
    /// error E0427: cannot use `ref` binding mode with ...
180
    CannotUseRefBindingModeWith(&'a str),
181
    /// error E0428: duplicate definition
182
    DuplicateDefinition(&'a str, Name),
183
    /// error E0429: `self` imports are only allowed within a { } list
184
    SelfImportsOnlyAllowedWithin,
185
    /// error E0430: `self` import can only appear once in the list
186
    SelfImportCanOnlyAppearOnceInTheList,
187
    /// error E0431: `self` import can only appear in an import list with a non-empty prefix
188
    SelfImportOnlyInImportListWithNonEmptyPrefix,
189
    /// error E0432: unresolved import
190
    UnresolvedImport(Option<(&'a str, &'a str)>),
191
    /// error E0433: failed to resolve
192
    FailedToResolve(&'a str),
193
    /// error E0434: can't capture dynamic environment in a fn item
194
    CannotCaptureDynamicEnvironmentInFnItem,
195
    /// error E0435: attempt to use a non-constant value in a constant
196
    AttemptToUseNonConstantValueInConstant,
197 198
}

199
/// Context of where `ResolutionError::UnresolvedName` arose.
200 201
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum UnresolvedNameContext {
202 203 204 205
    /// `PathIsMod(id)` indicates that a given path, used in
    /// expression context, actually resolved to a module rather than
    /// a value. The `id` attached to the variant is the node id of
    /// the erroneous path expression.
206
    PathIsMod(ast::NodeId),
207 208 209 210

    /// `Other` means we have no extra information about the context
    /// of the unresolved name error. (Maybe we could eliminate all
    /// such cases; but for now, this is an information-free default.)
211 212 213
    Other,
}

C
corentih 已提交
214 215 216
fn resolve_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
                                       span: syntax::codemap::Span,
                                       resolution_error: ResolutionError<'b>) {
N
Nick Cameron 已提交
217
    resolve_struct_error(resolver, span, resolution_error).emit();
N
Nick Cameron 已提交
218 219 220 221 222
}

fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
                                              span: syntax::codemap::Span,
                                              resolution_error: ResolutionError<'b>)
N
Nick Cameron 已提交
223
                                              -> DiagnosticBuilder<'a> {
224
    if !resolver.emit_errors {
N
Nick Cameron 已提交
225
        return resolver.session.diagnostic().struct_dummy();
226
    }
N
Nick Cameron 已提交
227

N
Nick Cameron 已提交
228
    match resolution_error {
229
        ResolutionError::TypeParametersFromOuterFunction => {
N
Nick Cameron 已提交
230 231 232 233 234
            struct_span_err!(resolver.session,
                             span,
                             E0401,
                             "can't use type parameters from outer function; try using a local \
                              type parameter instead")
C
corentih 已提交
235
        }
236
        ResolutionError::OuterTypeParameterContext => {
N
Nick Cameron 已提交
237 238 239 240
            struct_span_err!(resolver.session,
                             span,
                             E0402,
                             "cannot use an outer type parameter in this context")
C
corentih 已提交
241
        }
242
        ResolutionError::NameAlreadyUsedInTypeParameterList(name) => {
N
Nick Cameron 已提交
243 244 245 246 247 248
            struct_span_err!(resolver.session,
                             span,
                             E0403,
                             "the name `{}` is already used for a type parameter in this type \
                              parameter list",
                             name)
C
corentih 已提交
249
        }
250
        ResolutionError::IsNotATrait(name) => {
N
Nick Cameron 已提交
251
            struct_span_err!(resolver.session, span, E0404, "`{}` is not a trait", name)
C
corentih 已提交
252
        }
253
        ResolutionError::UndeclaredTraitName(name) => {
N
Nick Cameron 已提交
254 255 256 257 258
            struct_span_err!(resolver.session,
                             span,
                             E0405,
                             "use of undeclared trait name `{}`",
                             name)
C
corentih 已提交
259
        }
260
        ResolutionError::UndeclaredAssociatedType => {
N
Nick Cameron 已提交
261
            struct_span_err!(resolver.session, span, E0406, "undeclared associated type")
C
corentih 已提交
262
        }
263
        ResolutionError::MethodNotMemberOfTrait(method, trait_) => {
N
Nick Cameron 已提交
264 265 266 267 268 269
            struct_span_err!(resolver.session,
                             span,
                             E0407,
                             "method `{}` is not a member of trait `{}`",
                             method,
                             trait_)
C
corentih 已提交
270
        }
271
        ResolutionError::TypeNotMemberOfTrait(type_, trait_) => {
N
Nick Cameron 已提交
272 273 274 275 276 277
            struct_span_err!(resolver.session,
                             span,
                             E0437,
                             "type `{}` is not a member of trait `{}`",
                             type_,
                             trait_)
C
corentih 已提交
278
        }
279
        ResolutionError::ConstNotMemberOfTrait(const_, trait_) => {
N
Nick Cameron 已提交
280 281 282 283 284 285
            struct_span_err!(resolver.session,
                             span,
                             E0438,
                             "const `{}` is not a member of trait `{}`",
                             const_,
                             trait_)
C
corentih 已提交
286
        }
287
        ResolutionError::VariableNotBoundInPattern(variable_name, pattern_number) => {
N
Nick Cameron 已提交
288 289 290 291 292 293
            struct_span_err!(resolver.session,
                             span,
                             E0408,
                             "variable `{}` from pattern #1 is not bound in pattern #{}",
                             variable_name,
                             pattern_number)
C
corentih 已提交
294
        }
295
        ResolutionError::VariableBoundWithDifferentMode(variable_name, pattern_number) => {
N
Nick Cameron 已提交
296 297 298 299 300 301 302
            struct_span_err!(resolver.session,
                             span,
                             E0409,
                             "variable `{}` is bound with different mode in pattern #{} than in \
                              pattern #1",
                             variable_name,
                             pattern_number)
C
corentih 已提交
303
        }
304
        ResolutionError::VariableNotBoundInParentPattern(variable_name, pattern_number) => {
N
Nick Cameron 已提交
305 306 307 308 309 310
            struct_span_err!(resolver.session,
                             span,
                             E0410,
                             "variable `{}` from pattern #{} is not bound in pattern #1",
                             variable_name,
                             pattern_number)
C
corentih 已提交
311
        }
312
        ResolutionError::SelfUsedOutsideImplOrTrait => {
N
Nick Cameron 已提交
313 314 315 316
            struct_span_err!(resolver.session,
                             span,
                             E0411,
                             "use of `Self` outside of an impl or trait")
C
corentih 已提交
317
        }
318
        ResolutionError::UseOfUndeclared(kind, name) => {
N
Nick Cameron 已提交
319 320 321 322 323 324
            struct_span_err!(resolver.session,
                             span,
                             E0412,
                             "use of undeclared {} `{}`",
                             kind,
                             name)
C
corentih 已提交
325
        }
326
        ResolutionError::DeclarationShadowsEnumVariantOrUnitLikeStruct(name) => {
N
Nick Cameron 已提交
327 328 329 330 331 332
            struct_span_err!(resolver.session,
                             span,
                             E0413,
                             "declaration of `{}` shadows an enum variant \
                              or unit-like struct in scope",
                             name)
C
corentih 已提交
333
        }
334
        ResolutionError::OnlyIrrefutablePatternsAllowedHere(did, name) => {
N
Nick Cameron 已提交
335 336 337 338 339 340 341
            let mut err = struct_span_err!(resolver.session,
                                           span,
                                           E0414,
                                           "only irrefutable patterns allowed here");
            err.span_note(span,
                          "there already is a constant in scope sharing the same \
                           name as this pattern");
342
            if let Some(sp) = resolver.ast_map.span_if_local(did) {
N
Nick Cameron 已提交
343
                err.span_note(sp, "constant defined here");
344
            }
M
Manish Goregaokar 已提交
345 346
            if let Some(directive) = resolver.current_module
                                             .import_resolutions
C
corentih 已提交
347
                                             .borrow()
348 349
                                             .get(&(name, ValueNS)) {
                let item = resolver.ast_map.expect_item(directive.id);
N
Nick Cameron 已提交
350
                err.span_note(item.span, "constant imported here");
351
            }
N
Nick Cameron 已提交
352
            err
C
corentih 已提交
353
        }
354
        ResolutionError::IdentifierBoundMoreThanOnceInParameterList(identifier) => {
N
Nick Cameron 已提交
355 356 357 358 359
            struct_span_err!(resolver.session,
                             span,
                             E0415,
                             "identifier `{}` is bound more than once in this parameter list",
                             identifier)
C
corentih 已提交
360
        }
361
        ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(identifier) => {
N
Nick Cameron 已提交
362 363 364 365 366
            struct_span_err!(resolver.session,
                             span,
                             E0416,
                             "identifier `{}` is bound more than once in the same pattern",
                             identifier)
C
corentih 已提交
367
        }
368
        ResolutionError::StaticVariableReference => {
N
Nick Cameron 已提交
369 370 371 372 373
            struct_span_err!(resolver.session,
                             span,
                             E0417,
                             "static variables cannot be referenced in a pattern, use a \
                              `const` instead")
C
corentih 已提交
374
        }
375
        ResolutionError::NotAnEnumVariantStructOrConst(name) => {
N
Nick Cameron 已提交
376 377 378 379 380
            struct_span_err!(resolver.session,
                             span,
                             E0418,
                             "`{}` is not an enum variant, struct or const",
                             name)
C
corentih 已提交
381
        }
382
        ResolutionError::UnresolvedEnumVariantStructOrConst(name) => {
N
Nick Cameron 已提交
383 384 385 386 387
            struct_span_err!(resolver.session,
                             span,
                             E0419,
                             "unresolved enum variant, struct or const `{}`",
                             name)
C
corentih 已提交
388
        }
389
        ResolutionError::NotAnAssociatedConst(name) => {
N
Nick Cameron 已提交
390 391 392 393 394
            struct_span_err!(resolver.session,
                             span,
                             E0420,
                             "`{}` is not an associated const",
                             name)
C
corentih 已提交
395
        }
396
        ResolutionError::UnresolvedAssociatedConst(name) => {
N
Nick Cameron 已提交
397 398 399 400 401
            struct_span_err!(resolver.session,
                             span,
                             E0421,
                             "unresolved associated const `{}`",
                             name)
C
corentih 已提交
402
        }
403
        ResolutionError::DoesNotNameAStruct(name) => {
N
Nick Cameron 已提交
404 405 406 407 408
            struct_span_err!(resolver.session,
                             span,
                             E0422,
                             "`{}` does not name a structure",
                             name)
C
corentih 已提交
409
        }
410
        ResolutionError::StructVariantUsedAsFunction(path_name) => {
N
Nick Cameron 已提交
411 412 413 414 415 416
            struct_span_err!(resolver.session,
                             span,
                             E0423,
                             "`{}` is the name of a struct or struct variant, but this expression \
                             uses it like a function name",
                             path_name)
C
corentih 已提交
417
        }
418
        ResolutionError::SelfNotAvailableInStaticMethod => {
N
Nick Cameron 已提交
419 420 421 422 423
            struct_span_err!(resolver.session,
                             span,
                             E0424,
                             "`self` is not available in a static method. Maybe a `self` \
                             argument is missing?")
C
corentih 已提交
424
        }
425
        ResolutionError::UnresolvedName(path, msg, context) => {
N
Nick Cameron 已提交
426 427 428 429 430 431
            let mut err = struct_span_err!(resolver.session,
                                           span,
                                           E0425,
                                           "unresolved name `{}`{}",
                                           path,
                                           msg);
432 433

            match context {
434
                UnresolvedNameContext::Other => { } // no help available
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
                UnresolvedNameContext::PathIsMod(id) => {
                    let mut help_msg = String::new();
                    let parent_id = resolver.ast_map.get_parent_node(id);
                    if let Some(hir_map::Node::NodeExpr(e)) = resolver.ast_map.find(parent_id) {
                        match e.node {
                            ExprField(_, ident) => {
                                help_msg = format!("To reference an item from the \
                                                    `{module}` module, use \
                                                    `{module}::{ident}`",
                                                   module = &*path,
                                                   ident = ident.node);
                            }
                            ExprMethodCall(ident, _, _) => {
                                help_msg = format!("To call a function from the \
                                                    `{module}` module, use \
                                                    `{module}::{ident}(..)`",
                                                   module = &*path,
                                                   ident = ident.node);
                            }
454 455 456 457 458
                            ExprCall(_, _) => {
                                help_msg = format!("No function corresponds to `{module}(..)`",
                                                   module = &*path);
                            }
                            _ => { } // no help available
459
                        }
460 461 462
                    } else {
                        help_msg = format!("Module `{module}` cannot be the value of an expression",
                                           module = &*path);
463 464 465
                    }

                    if !help_msg.is_empty() {
N
Nick Cameron 已提交
466
                        err.fileline_help(span, &help_msg);
467 468 469
                    }
                }
            }
N
Nick Cameron 已提交
470
            err
C
corentih 已提交
471
        }
472
        ResolutionError::UndeclaredLabel(name) => {
N
Nick Cameron 已提交
473 474 475 476 477
            struct_span_err!(resolver.session,
                             span,
                             E0426,
                             "use of undeclared label `{}`",
                             name)
C
corentih 已提交
478
        }
479
        ResolutionError::CannotUseRefBindingModeWith(descr) => {
N
Nick Cameron 已提交
480 481 482 483 484
            struct_span_err!(resolver.session,
                             span,
                             E0427,
                             "cannot use `ref` binding mode with {}",
                             descr)
C
corentih 已提交
485
        }
486
        ResolutionError::DuplicateDefinition(namespace, name) => {
N
Nick Cameron 已提交
487 488 489 490 491 492
            struct_span_err!(resolver.session,
                             span,
                             E0428,
                             "duplicate definition of {} `{}`",
                             namespace,
                             name)
C
corentih 已提交
493
        }
494
        ResolutionError::SelfImportsOnlyAllowedWithin => {
N
Nick Cameron 已提交
495 496 497 498 499
            struct_span_err!(resolver.session,
                             span,
                             E0429,
                             "{}",
                             "`self` imports are only allowed within a { } list")
C
corentih 已提交
500
        }
501
        ResolutionError::SelfImportCanOnlyAppearOnceInTheList => {
N
Nick Cameron 已提交
502 503 504 505
            struct_span_err!(resolver.session,
                             span,
                             E0430,
                             "`self` import can only appear once in the list")
C
corentih 已提交
506
        }
507
        ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
N
Nick Cameron 已提交
508 509 510 511 512
            struct_span_err!(resolver.session,
                             span,
                             E0431,
                             "`self` import can only appear in an import list with a \
                              non-empty prefix")
513
        }
514
        ResolutionError::UnresolvedImport(name) => {
515
            let msg = match name {
516
                Some((n, p)) => format!("unresolved import `{}`{}", n, p),
C
corentih 已提交
517
                None => "unresolved import".to_owned(),
518
            };
N
Nick Cameron 已提交
519
            struct_span_err!(resolver.session, span, E0432, "{}", msg)
C
corentih 已提交
520
        }
521
        ResolutionError::FailedToResolve(msg) => {
N
Nick Cameron 已提交
522
            struct_span_err!(resolver.session, span, E0433, "failed to resolve. {}", msg)
C
corentih 已提交
523
        }
524
        ResolutionError::CannotCaptureDynamicEnvironmentInFnItem => {
N
Nick Cameron 已提交
525 526 527 528 529 530
            struct_span_err!(resolver.session,
                             span,
                             E0434,
                             "{}",
                             "can't capture dynamic environment in a fn item; use the || { ... } \
                              closure form instead")
C
corentih 已提交
531 532
        }
        ResolutionError::AttemptToUseNonConstantValueInConstant => {
N
Nick Cameron 已提交
533 534 535 536
            struct_span_err!(resolver.session,
                             span,
                             E0435,
                             "attempt to use a non-constant value in a constant")
C
corentih 已提交
537
        }
N
Nick Cameron 已提交
538
    }
539 540
}

N
Niko Matsakis 已提交
541
#[derive(Copy, Clone)]
542
struct BindingInfo {
543
    span: Span,
544
    binding_mode: BindingMode,
545 546 547
}

// Map from the name in a pattern to its binding mode.
548
type BindingMap = HashMap<Name, BindingInfo>;
549

N
Niko Matsakis 已提交
550
#[derive(Copy, Clone, PartialEq)]
F
Felix S. Klock II 已提交
551
enum PatternBindingMode {
552
    RefutableMode,
553
    LocalIrrefutableMode,
554
    ArgumentIrrefutableMode,
555 556
}

N
Niko Matsakis 已提交
557
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
G
Garming Sam 已提交
558
pub enum Namespace {
559
    TypeNS,
C
corentih 已提交
560
    ValueNS,
561 562
}

563
impl<'a, 'v, 'tcx> Visitor<'v> for Resolver<'a, 'tcx> {
564 565 566
    fn visit_nested_item(&mut self, item: hir::ItemId) {
        self.visit_item(self.ast_map.expect_item(item.id))
    }
567
    fn visit_item(&mut self, item: &Item) {
568
        execute_callback!(hir_map::Node::NodeItem(item), self);
A
Alex Crichton 已提交
569
        self.resolve_item(item);
570
    }
571
    fn visit_arm(&mut self, arm: &Arm) {
A
Alex Crichton 已提交
572
        self.resolve_arm(arm);
573
    }
574
    fn visit_block(&mut self, block: &Block) {
575
        execute_callback!(hir_map::Node::NodeBlock(block), self);
A
Alex Crichton 已提交
576
        self.resolve_block(block);
577
    }
578
    fn visit_expr(&mut self, expr: &Expr) {
579
        execute_callback!(hir_map::Node::NodeExpr(expr), self);
A
Alex Crichton 已提交
580
        self.resolve_expr(expr);
581
    }
582
    fn visit_local(&mut self, local: &Local) {
583
        execute_callback!(hir_map::Node::NodeLocal(&*local.pat), self);
A
Alex Crichton 已提交
584
        self.resolve_local(local);
585
    }
586
    fn visit_ty(&mut self, ty: &Ty) {
A
Alex Crichton 已提交
587
        self.resolve_type(ty);
588
    }
589 590 591
    fn visit_generics(&mut self, generics: &Generics) {
        self.resolve_generics(generics);
    }
C
corentih 已提交
592
    fn visit_poly_trait_ref(&mut self, tref: &hir::PolyTraitRef, m: &hir::TraitBoundModifier) {
593 594
        match self.resolve_trait_reference(tref.trait_ref.ref_id, &tref.trait_ref.path, 0) {
            Ok(def) => self.record_def(tref.trait_ref.ref_id, def),
C
corentih 已提交
595 596
            Err(_) => {
                // error already reported
597
                self.record_def(tref.trait_ref.ref_id, err_path_resolution())
C
corentih 已提交
598
            }
599
        }
600
        intravisit::walk_poly_trait_ref(self, tref, m);
601
    }
C
corentih 已提交
602 603 604 605
    fn visit_variant(&mut self,
                     variant: &hir::Variant,
                     generics: &Generics,
                     item_id: ast::NodeId) {
606
        execute_callback!(hir_map::Node::NodeVariant(variant), self);
607 608 609
        if let Some(ref dis_expr) = variant.node.disr_expr {
            // resolve the discriminator expr as a constant
            self.with_constant_rib(|this| {
610
                this.visit_expr(dis_expr);
611 612 613
            });
        }

614
        // `intravisit::walk_variant` without the discriminant expression.
C
corentih 已提交
615 616 617 618 619
        self.visit_variant_data(&variant.node.data,
                                variant.node.name,
                                generics,
                                item_id,
                                variant.span);
620
    }
621 622
    fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem) {
        execute_callback!(hir_map::Node::NodeForeignItem(foreign_item), self);
623 624 625 626
        let type_parameters = match foreign_item.node {
            ForeignItemFn(_, ref generics) => {
                HasTypeParameters(generics, FnSpace, ItemRibKind)
            }
C
corentih 已提交
627
            ForeignItemStatic(..) => NoTypeParameters,
628 629
        };
        self.with_type_parameter_rib(type_parameters, |this| {
630
            intravisit::walk_foreign_item(this, foreign_item);
631 632 633
        });
    }
    fn visit_fn(&mut self,
634
                function_kind: FnKind<'v>,
635 636 637 638 639
                declaration: &'v FnDecl,
                block: &'v Block,
                _: Span,
                node_id: NodeId) {
        let rib_kind = match function_kind {
640
            FnKind::ItemFn(_, generics, _, _, _, _) => {
641 642 643
                self.visit_generics(generics);
                ItemRibKind
            }
644
            FnKind::Method(_, sig, _) => {
645 646
                self.visit_generics(&sig.generics);
                self.visit_explicit_self(&sig.explicit_self);
647 648
                MethodRibKind
            }
V
Vadim Petrochenkov 已提交
649
            FnKind::Closure => ClosureRibKind(node_id),
650 651 652
        };
        self.resolve_function(rib_kind, declaration, block);
    }
653
}
654

655 656
type ErrorMessage = Option<(Span, String)>;

657
#[derive(Clone, PartialEq, Eq)]
F
Felix S. Klock II 已提交
658
enum ResolveResult<T> {
C
corentih 已提交
659 660 661
    Failed(ErrorMessage), // Failed to resolve the name, optional helpful error message.
    Indeterminate, // Couldn't determine due to unresolved globs.
    Success(T), // Successfully resolved the import.
662 663
}

664
impl<T> ResolveResult<T> {
665 666 667 668 669
    fn and_then<U, F: FnOnce(T) -> ResolveResult<U>>(self, f: F) -> ResolveResult<U> {
        match self {
            Failed(msg) => Failed(msg),
            Indeterminate => Indeterminate,
            Success(t) => f(t),
C
corentih 已提交
670
        }
671 672 673
    }
}

674 675 676 677
enum FallbackSuggestion {
    NoSuggestion,
    Field,
    Method,
678
    TraitItem,
679
    StaticMethod(String),
680
    TraitMethod(String),
681 682
}

N
Niko Matsakis 已提交
683
#[derive(Copy, Clone)]
684
enum TypeParameters<'tcx, 'a> {
685
    NoTypeParameters,
C
corentih 已提交
686 687
    HasTypeParameters(// Type parameters.
                      &'a Generics,
688

C
corentih 已提交
689 690 691
                      // Identifies the things that these parameters
                      // were declared on (type, fn, etc)
                      ParamSpace,
692

C
corentih 已提交
693
                      // The kind of the rib used for type parameters.
694
                      RibKind<'tcx>),
695 696
}

697
// The rib kind controls the translation of local
698
// definitions (`Def::Local`) to upvars (`Def::Upvar`).
N
Niko Matsakis 已提交
699
#[derive(Copy, Clone, Debug)]
700
enum RibKind<'a> {
701 702
    // No translation needs to be applied.
    NormalRibKind,
703

704 705
    // We passed through a closure scope at the given node ID.
    // Translate upvars as appropriate.
706
    ClosureRibKind(NodeId /* func id */),
707

708
    // We passed through an impl or trait and are now in one of its
709
    // methods. Allow references to ty params that impl or trait
710 711
    // binds. Disallow any other upvars (including other ty params that are
    // upvars).
712
    MethodRibKind,
713

714 715
    // We passed through an item scope. Disallow upvars.
    ItemRibKind,
716 717

    // We're in a constant item. Can't refer to dynamic stuff.
C
corentih 已提交
718
    ConstantItemRibKind,
719 720 721

    // We passed through an anonymous module.
    AnonymousModuleRibKind(Module<'a>),
722 723
}

N
Niko Matsakis 已提交
724
#[derive(Copy, Clone)]
F
Felix S. Klock II 已提交
725
enum UseLexicalScopeFlag {
726
    DontUseLexicalScope,
C
corentih 已提交
727
    UseLexicalScope,
728 729
}

730
enum ModulePrefixResult<'a> {
731
    NoPrefixFound,
732
    PrefixFound(Module<'a>, usize),
733 734
}

735 736 737 738 739 740 741 742 743
#[derive(Copy, Clone)]
enum AssocItemResolveResult {
    /// Syntax such as `<T>::item`, which can't be resolved until type
    /// checking.
    TypecheckRequired,
    /// We should have been able to resolve the associated item.
    ResolveAttempt(Option<PathResolution>),
}

N
Niko Matsakis 已提交
744
#[derive(Copy, Clone)]
F
Felix S. Klock II 已提交
745
enum BareIdentifierPatternResolution {
746
    FoundStructOrEnumVariant(Def, LastPrivate),
747
    FoundConst(Def, LastPrivate, Name),
C
corentih 已提交
748
    BareIdentifierPatternUnresolved,
749 750
}

751
/// One local scope.
J
Jorge Aparicio 已提交
752
#[derive(Debug)]
753
struct Rib<'a> {
754
    bindings: HashMap<Name, DefLike>,
755
    kind: RibKind<'a>,
B
Brian Anderson 已提交
756
}
757

758 759
impl<'a> Rib<'a> {
    fn new(kind: RibKind<'a>) -> Rib<'a> {
760
        Rib {
761
            bindings: HashMap::new(),
C
corentih 已提交
762
            kind: kind,
763
        }
764 765 766
    }
}

767 768 769
/// A definition along with the index of the rib it was found on
struct LocalDef {
    ribs: Option<(Namespace, usize)>,
C
corentih 已提交
770
    def: Def,
771 772 773 774 775 776
}

impl LocalDef {
    fn from_def(def: Def) -> Self {
        LocalDef {
            ribs: None,
C
corentih 已提交
777
            def: def,
778 779 780 781
        }
    }
}

782
/// The link from a module up to its nearest parent node.
J
Jorge Aparicio 已提交
783
#[derive(Clone,Debug)]
784
enum ParentLink<'a> {
785
    NoParentLink,
786 787
    ModuleParentLink(Module<'a>, Name),
    BlockParentLink(Module<'a>, NodeId),
788 789
}

790
/// One node in the tree of modules.
791 792
pub struct ModuleS<'a> {
    parent_link: ParentLink<'a>,
J
Jeffrey Seyfried 已提交
793
    def: Option<Def>,
794
    is_public: bool,
795
    is_extern_crate: bool,
796

797
    children: RefCell<HashMap<(Name, Namespace), NameBinding<'a>>>,
E
Eduard Burtescu 已提交
798
    imports: RefCell<Vec<ImportDirective>>,
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

    // The anonymous children of this node. Anonymous children are pseudo-
    // modules that are implicitly created around items contained within
    // blocks.
    //
    // For example, if we have this:
    //
    //  fn f() {
    //      fn g() {
    //          ...
    //      }
    //  }
    //
    // There will be an anonymous module created around `g` with the ID of the
    // entry block for `f`.
814
    anonymous_children: RefCell<NodeMap<Module<'a>>>,
815 816

    // The status of resolving each import in this module.
817
    import_resolutions: RefCell<HashMap<(Name, Namespace), ImportResolution<'a>>>,
818 819

    // The number of unresolved globs that this module exports.
820
    glob_count: Cell<usize>,
821

822 823 824 825 826 827
    // The number of unresolved pub imports (both regular and globs) in this module
    pub_count: Cell<usize>,

    // The number of unresolved pub glob imports in this module
    pub_glob_count: Cell<usize>,

828
    // The index of the import we're resolving.
829
    resolved_import_count: Cell<usize>,
830 831 832 833

    // Whether this module is populated. If not populated, any attempt to
    // access the children must be preceded with a
    // `populate_module_if_necessary` call.
834
    populated: Cell<bool>,
835 836
}

837 838 839 840 841
pub type Module<'a> = &'a ModuleS<'a>;

impl<'a> ModuleS<'a> {
    fn new(parent_link: ParentLink<'a>, def: Option<Def>, external: bool, is_public: bool) -> Self {
        ModuleS {
842
            parent_link: parent_link,
J
Jeffrey Seyfried 已提交
843
            def: def,
844
            is_public: is_public,
845
            is_extern_crate: false,
846
            children: RefCell::new(HashMap::new()),
847
            imports: RefCell::new(Vec::new()),
848
            anonymous_children: RefCell::new(NodeMap()),
849
            import_resolutions: RefCell::new(HashMap::new()),
850
            glob_count: Cell::new(0),
851 852
            pub_count: Cell::new(0),
            pub_glob_count: Cell::new(0),
853
            resolved_import_count: Cell::new(0),
854
            populated: Cell::new(!external),
855
        }
B
Brian Anderson 已提交
856 857
    }

858 859 860 861
    fn get_child(&self, name: Name, ns: Namespace) -> Option<NameBinding<'a>> {
        self.children.borrow().get(&(name, ns)).cloned()
    }

862 863 864 865
    // If the name is not yet defined, define the name and return None.
    // Otherwise, return the existing definition.
    fn try_define_child(&self, name: Name, ns: Namespace, binding: NameBinding<'a>)
                        -> Option<NameBinding<'a>> {
866
        match self.children.borrow_mut().entry((name, ns)) {
867 868 869 870 871 872 873 874 875 876
            hash_map::Entry::Vacant(entry) => { entry.insert(binding); None }
            hash_map::Entry::Occupied(entry) => { Some(entry.get().clone()) },
        }
    }

    fn for_each_local_child<F: FnMut(Name, Namespace, &NameBinding<'a>)>(&self, mut f: F) {
        for (&(name, ns), name_binding) in self.children.borrow().iter() {
            if !name_binding.is_extern_crate() {
                f(name, ns, name_binding)
            }
877 878 879
        }
    }

880
    fn def_id(&self) -> Option<DefId> {
J
Jeffrey Seyfried 已提交
881
        self.def.as_ref().map(Def::def_id)
882 883 884
    }

    fn is_normal(&self) -> bool {
J
Jeffrey Seyfried 已提交
885
        match self.def {
886
            Some(Def::Mod(_)) | Some(Def::ForeignMod(_)) => true,
887 888 889 890 891
            _ => false,
        }
    }

    fn is_trait(&self) -> bool {
J
Jeffrey Seyfried 已提交
892
        match self.def {
893
            Some(Def::Trait(_)) => true,
894
            _ => false,
895
        }
B
Brian Anderson 已提交
896 897
    }

F
Felix S. Klock II 已提交
898
    fn all_imports_resolved(&self) -> bool {
899 900 901 902 903 904
        if self.imports.borrow_state() == ::std::cell::BorrowState::Writing {
            // it is currently being resolved ! so nope
            false
        } else {
            self.imports.borrow().len() == self.resolved_import_count.get()
        }
905 906
    }

V
Victor Berger 已提交
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
    pub fn inc_glob_count(&self) {
        self.glob_count.set(self.glob_count.get() + 1);
    }
    pub fn dec_glob_count(&self) {
        assert!(self.glob_count.get() > 0);
        self.glob_count.set(self.glob_count.get() - 1);
    }
    pub fn inc_pub_count(&self) {
        self.pub_count.set(self.pub_count.get() + 1);
    }
    pub fn dec_pub_count(&self) {
        assert!(self.pub_count.get() > 0);
        self.pub_count.set(self.pub_count.get() - 1);
    }
    pub fn inc_pub_glob_count(&self) {
        self.pub_glob_count.set(self.pub_glob_count.get() + 1);
    }
    pub fn dec_pub_glob_count(&self) {
        assert!(self.pub_glob_count.get() > 0);
        self.pub_glob_count.set(self.pub_glob_count.get() - 1);
    }
}

930
impl<'a> fmt::Debug for ModuleS<'a> {
931
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
C
corentih 已提交
932
        write!(f,
933 934
               "{:?}, {}",
               self.def,
C
corentih 已提交
935 936 937 938 939
               if self.is_public {
                   "public"
               } else {
                   "private"
               })
940 941 942
    }
}

943
bitflags! {
J
Jorge Aparicio 已提交
944
    #[derive(Debug)]
945
    flags DefModifiers: u8 {
V
Vadim Petrochenkov 已提交
946 947
        // Enum variants are always considered `PUBLIC`, this is needed for `use Enum::Variant`
        // or `use Enum::*` to work on private enums.
T
Fallout  
Tamir Duberstein 已提交
948 949
        const PUBLIC     = 1 << 0,
        const IMPORTABLE = 1 << 1,
V
Vadim Petrochenkov 已提交
950
        // Variants are considered `PUBLIC`, but some of them live in private enums.
951 952
        // We need to track them to prohibit reexports like `pub use PrivEnum::Variant`.
        const PRIVATE_VARIANT = 1 << 2,
J
Jeffrey Seyfried 已提交
953
        const PRELUDE = 1 << 3,
954 955 956
    }
}

957
// Records a possibly-private value, type, or module definition.
958 959 960
#[derive(Clone, Debug)]
pub struct NameBinding<'a> {
    modifiers: DefModifiers, // see note in ImportResolution about how to use this
961
    def_or_module: DefOrModule<'a>,
962
    span: Option<Span>,
963 964
}

965
#[derive(Clone, Debug)]
966
enum DefOrModule<'a> {
967
    Def(Def),
968
    Module(Module<'a>),
969 970
}

971
impl<'a> NameBinding<'a> {
972
    fn create_from_module(module: Module<'a>, span: Option<Span>) -> Self {
973
        let modifiers = if module.is_public {
T
Fallout  
Tamir Duberstein 已提交
974 975 976 977
            DefModifiers::PUBLIC
        } else {
            DefModifiers::empty()
        } | DefModifiers::IMPORTABLE;
978

979
        NameBinding { modifiers: modifiers, def_or_module: DefOrModule::Module(module), span: span }
980 981
    }

982
    fn module(&self) -> Option<Module<'a>> {
983
        match self.def_or_module {
984
            DefOrModule::Module(ref module) => Some(module),
985
            DefOrModule::Def(_) => None,
986 987 988
        }
    }

989
    fn def(&self) -> Option<Def> {
990 991
        match self.def_or_module {
            DefOrModule::Def(def) => Some(def),
J
Jeffrey Seyfried 已提交
992
            DefOrModule::Module(ref module) => module.def,
993
        }
994
    }
995

996
    fn defined_with(&self, modifiers: DefModifiers) -> bool {
997
        self.modifiers.contains(modifiers)
998 999 1000 1001 1002 1003 1004 1005 1006 1007
    }

    fn is_public(&self) -> bool {
        self.defined_with(DefModifiers::PUBLIC)
    }

    fn def_and_lp(&self) -> (Def, LastPrivate) {
        let def = self.def().unwrap();
        (def, LastMod(if self.is_public() { AllPublic } else { DependsOn(def.def_id()) }))
    }
1008 1009 1010 1011

    fn is_extern_crate(&self) -> bool {
        self.module().map(|module| module.is_extern_crate).unwrap_or(false)
    }
1012 1013
}

1014
/// Interns the names of the primitive types.
F
Felix S. Klock II 已提交
1015
struct PrimitiveTypeTable {
1016
    primitive_types: HashMap<Name, PrimTy>,
1017
}
1018

1019
impl PrimitiveTypeTable {
K
Kevin Butler 已提交
1020
    fn new() -> PrimitiveTypeTable {
C
corentih 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
        let mut table = PrimitiveTypeTable { primitive_types: HashMap::new() };

        table.intern("bool", TyBool);
        table.intern("char", TyChar);
        table.intern("f32", TyFloat(TyF32));
        table.intern("f64", TyFloat(TyF64));
        table.intern("isize", TyInt(TyIs));
        table.intern("i8", TyInt(TyI8));
        table.intern("i16", TyInt(TyI16));
        table.intern("i32", TyInt(TyI32));
        table.intern("i64", TyInt(TyI64));
        table.intern("str", TyStr);
        table.intern("usize", TyUint(TyUs));
        table.intern("u8", TyUint(TyU8));
        table.intern("u16", TyUint(TyU16));
        table.intern("u32", TyUint(TyU32));
        table.intern("u64", TyUint(TyU64));
K
Kevin Butler 已提交
1038 1039 1040 1041

        table
    }

1042
    fn intern(&mut self, string: &str, primitive_type: PrimTy) {
1043
        self.primitive_types.insert(token::intern(string), primitive_type);
1044 1045 1046
    }
}

1047
/// The main resolver class.
C
corentih 已提交
1048
pub struct Resolver<'a, 'tcx: 'a> {
E
Eduard Burtescu 已提交
1049
    session: &'a Session,
1050

1051
    ast_map: &'a hir_map::Map<'tcx>,
1052

1053
    graph_root: Module<'a>,
1054

1055
    trait_item_map: FnvHashMap<(Name, DefId), DefId>,
1056

1057
    structs: FnvHashMap<DefId, Vec<Name>>,
1058

1059
    // The number of imports that are currently unresolved.
1060
    unresolved_imports: usize,
1061 1062

    // The module that represents the current item scope.
1063
    current_module: Module<'a>,
1064 1065

    // The current set of local scopes, for values.
1066
    // FIXME #4948: Reuse ribs to avoid allocation.
1067
    value_ribs: Vec<Rib<'a>>,
1068 1069

    // The current set of local scopes, for types.
1070
    type_ribs: Vec<Rib<'a>>,
1071

1072
    // The current set of local scopes, for labels.
1073
    label_ribs: Vec<Rib<'a>>,
1074

1075
    // The trait that the current context can refer to.
1076 1077 1078 1079
    current_trait_ref: Option<(DefId, TraitRef)>,

    // The current self type if inside an impl (used for better errors).
    current_self_type: Option<Ty>,
1080

1081
    // The idents for the primitive types.
E
Eduard Burtescu 已提交
1082
    primitive_type_table: PrimitiveTypeTable,
1083

J
Jonathan S 已提交
1084
    def_map: RefCell<DefMap>,
1085 1086
    freevars: FreevarMap,
    freevars_seen: NodeMap<NodeMap<usize>>,
1087
    export_map: ExportMap,
1088
    trait_map: TraitMap,
1089
    external_exports: ExternalExports,
1090

1091 1092 1093 1094 1095
    // Whether or not to print error messages. Can be set to true
    // when getting additional info for error message suggestions,
    // so as to avoid printing duplicate errors
    emit_errors: bool,

1096 1097 1098 1099 1100
    make_glob_map: bool,
    // Maps imports to the names of items actually imported (this actually maps
    // all imports, but only glob imports are actually interesting).
    glob_map: GlobMap,

1101
    used_imports: HashSet<(NodeId, Namespace)>,
1102
    used_crates: HashSet<CrateNum>,
G
Garming Sam 已提交
1103 1104

    // Callback function for intercepting walks
1105
    callback: Option<Box<Fn(hir_map::Node, &mut bool) -> bool>>,
G
Garming Sam 已提交
1106 1107 1108
    // The intention is that the callback modifies this flag.
    // Once set, the resolver falls out of the walk, preserving the ribs.
    resolved: bool,
1109 1110 1111 1112 1113 1114

    arenas: &'a ResolverArenas<'a>,
}

pub struct ResolverArenas<'a> {
    modules: arena::TypedArena<ModuleS<'a>>,
1115 1116
}

1117
#[derive(PartialEq)]
S
Steven Fackler 已提交
1118 1119
enum FallbackChecks {
    Everything,
C
corentih 已提交
1120
    OnlyTraitAndStatics,
S
Steven Fackler 已提交
1121 1122
}

1123 1124
impl<'a, 'tcx> Resolver<'a, 'tcx> {
    fn new(session: &'a Session,
1125
           ast_map: &'a hir_map::Map<'tcx>,
1126 1127
           make_glob_map: MakeGlobMap,
           arenas: &'a ResolverArenas<'a>)
C
corentih 已提交
1128
           -> Resolver<'a, 'tcx> {
1129
        let root_def_id = ast_map.local_def_id(CRATE_NODE_ID);
1130
        let graph_root = ModuleS::new(NoParentLink, Some(Def::Mod(root_def_id)), false, true);
1131
        let graph_root = arenas.modules.alloc(graph_root);
K
Kevin Butler 已提交
1132 1133 1134 1135

        Resolver {
            session: session,

1136 1137
            ast_map: ast_map,

K
Kevin Butler 已提交
1138 1139
            // The outermost module has def ID 0; this is not reflected in the
            // AST.
1140
            graph_root: graph_root,
K
Kevin Butler 已提交
1141

1142 1143
            trait_item_map: FnvHashMap(),
            structs: FnvHashMap(),
K
Kevin Butler 已提交
1144 1145 1146

            unresolved_imports: 0,

1147
            current_module: graph_root,
1148 1149 1150
            value_ribs: Vec::new(),
            type_ribs: Vec::new(),
            label_ribs: Vec::new(),
K
Kevin Butler 已提交
1151 1152 1153 1154 1155 1156

            current_trait_ref: None,
            current_self_type: None,

            primitive_type_table: PrimitiveTypeTable::new(),

1157
            def_map: RefCell::new(NodeMap()),
1158 1159
            freevars: NodeMap(),
            freevars_seen: NodeMap(),
1160 1161
            export_map: NodeMap(),
            trait_map: NodeMap(),
K
Kevin Butler 已提交
1162
            used_imports: HashSet::new(),
1163
            used_crates: HashSet::new(),
1164
            external_exports: DefIdSet(),
K
Kevin Butler 已提交
1165 1166

            emit_errors: true,
1167 1168
            make_glob_map: make_glob_map == MakeGlobMap::Yes,
            glob_map: HashMap::new(),
G
Garming Sam 已提交
1169 1170 1171

            callback: None,
            resolved: false,
1172 1173 1174 1175 1176 1177 1178 1179

            arenas: arenas,
        }
    }

    fn arenas() -> ResolverArenas<'a> {
        ResolverArenas {
            modules: arena::TypedArena::new(),
K
Kevin Butler 已提交
1180 1181
        }
    }
1182

1183 1184 1185 1186 1187 1188 1189 1190
    fn new_module(&self,
                  parent_link: ParentLink<'a>,
                  def: Option<Def>,
                  external: bool,
                  is_public: bool) -> Module<'a> {
        self.arenas.modules.alloc(ModuleS::new(parent_link, def, external, is_public))
    }

1191 1192 1193 1194 1195 1196
    fn new_extern_crate_module(&self, parent_link: ParentLink<'a>, def: Def) -> Module<'a> {
        let mut module = ModuleS::new(parent_link, Some(def), false, true);
        module.is_extern_crate = true;
        self.arenas.modules.alloc(module)
    }

1197 1198 1199 1200
    fn get_ribs<'b>(&'b mut self, ns: Namespace) -> &'b mut Vec<Rib<'a>> {
        match ns { ValueNS => &mut self.value_ribs, TypeNS => &mut self.type_ribs }
    }

1201
    #[inline]
1202 1203 1204 1205
    fn record_import_use(&mut self, name: Name, ns: Namespace, resolution: &ImportResolution<'a>) {
        let import_id = resolution.id;
        self.used_imports.insert((import_id, ns));

1206 1207 1208 1209
        if !self.make_glob_map {
            return;
        }
        if self.glob_map.contains_key(&import_id) {
1210
            self.glob_map.get_mut(&import_id).unwrap().insert(name);
1211 1212 1213 1214 1215 1216 1217 1218 1219
            return;
        }

        let mut new_set = HashSet::new();
        new_set.insert(name);
        self.glob_map.insert(import_id, new_set);
    }

    fn get_trait_name(&self, did: DefId) -> Name {
1220 1221
        if let Some(node_id) = self.ast_map.as_local_node_id(did) {
            self.ast_map.expect_item(node_id).name
1222
        } else {
1223
            self.session.cstore.item_name(did)
1224 1225 1226
        }
    }

1227
    /// Resolves the given module path from the given root `module_`.
F
Felix S. Klock II 已提交
1228
    fn resolve_module_path_from_root(&mut self,
1229
                                     module_: Module<'a>,
1230
                                     module_path: &[Name],
1231
                                     index: usize,
1232 1233
                                     span: Span,
                                     lp: LastPrivate)
1234
                                     -> ResolveResult<(Module<'a>, LastPrivate)> {
1235 1236 1237 1238
        fn search_parent_externals<'a>(needle: Name, module: Module<'a>) -> Option<Module<'a>> {
            match module.get_child(needle, TypeNS) {
                Some(ref binding) if binding.is_extern_crate() => Some(module),
                _ => match module.parent_link {
1239
                    ModuleParentLink(ref parent, _) => {
1240
                        search_parent_externals(needle, parent)
1241
                    }
C
corentih 已提交
1242 1243
                    _ => None,
                },
1244
            }
1245 1246
        }

1247
        let mut search_module = module_;
1248
        let mut index = index;
A
Alex Crichton 已提交
1249
        let module_path_len = module_path.len();
1250
        let mut closest_private = lp;
1251 1252 1253 1254 1255

        // Resolve the module part of the path. This does not involve looking
        // upward though scope chains; we simply resolve names directly in
        // modules as we go.
        while index < module_path_len {
A
Alex Crichton 已提交
1256
            let name = module_path[index];
1257
            match self.resolve_name_in_module(search_module, name, TypeNS, false, true) {
1258
                Failed(None) => {
1259
                    let segment_name = name.as_str();
1260
                    let module_name = module_to_string(search_module);
1261
                    let mut span = span;
1262
                    let msg = if "???" == &module_name {
1263
                        span.hi = span.lo + Pos::from_usize(segment_name.len());
1264

C
corentih 已提交
1265
                        match search_parent_externals(name, &self.current_module) {
1266
                            Some(module) => {
1267 1268
                                let path_str = names_to_string(module_path);
                                let target_mod_str = module_to_string(&*module);
C
corentih 已提交
1269
                                let current_mod_str = module_to_string(&*self.current_module);
1270 1271 1272 1273 1274 1275 1276

                                let prefix = if target_mod_str == current_mod_str {
                                    "self::".to_string()
                                } else {
                                    format!("{}::", target_mod_str)
                                };

1277
                                format!("Did you mean `{}{}`?", prefix, path_str)
C
corentih 已提交
1278 1279
                            }
                            None => format!("Maybe a missing `extern crate {}`?", segment_name),
1280
                        }
1281
                    } else {
C
corentih 已提交
1282
                        format!("Could not find `{}` in `{}`", segment_name, module_name)
1283
                    };
1284

1285
                    return Failed(Some((span, msg)));
1286
                }
1287
                Failed(err) => return Failed(err),
B
Brian Anderson 已提交
1288
                Indeterminate => {
C
corentih 已提交
1289 1290 1291
                    debug!("(resolving module path for import) module resolution is \
                            indeterminate: {}",
                           name);
B
Brian Anderson 已提交
1292
                    return Indeterminate;
1293
                }
J
Jeffrey Seyfried 已提交
1294
                Success((binding, used_proxy)) => {
1295 1296
                    // Check to see whether there are type bindings, and, if
                    // so, whether there is a module within.
J
Jeffrey Seyfried 已提交
1297
                    if let Some(module_def) = binding.module() {
1298
                        search_module = module_def;
1299

1300 1301 1302
                        // Keep track of the closest private module used
                        // when resolving this import chain.
                        if !used_proxy && !search_module.is_public {
1303
                            if let Some(did) = search_module.def_id() {
1304
                                closest_private = LastMod(DependsOn(did));
1305 1306
                            }
                        }
1307 1308 1309
                    } else {
                        let msg = format!("Not a module `{}`", name);
                        return Failed(Some((span, msg)));
1310 1311 1312 1313
                    }
                }
            }

T
Tim Chevalier 已提交
1314
            index += 1;
1315 1316
        }

1317
        return Success((search_module, closest_private));
1318 1319
    }

1320 1321
    /// Attempts to resolve the module part of an import directive or path
    /// rooted at the given module.
1322 1323 1324
    ///
    /// On success, returns the resolved module, and the closest *private*
    /// module found to the destination when resolving this path.
F
Felix S. Klock II 已提交
1325
    fn resolve_module_path(&mut self,
1326
                           module_: Module<'a>,
1327
                           module_path: &[Name],
1328
                           use_lexical_scope: UseLexicalScopeFlag,
J
Jeffrey Seyfried 已提交
1329
                           span: Span)
1330
                           -> ResolveResult<(Module<'a>, LastPrivate)> {
1331 1332 1333
        if module_path.len() == 0 {
            return Success((self.graph_root, LastMod(AllPublic))) // Use the crate root
        }
1334

1335
        debug!("(resolving module path for import) processing `{}` rooted at `{}`",
1336 1337
               names_to_string(module_path),
               module_to_string(&*module_));
1338

1339
        // Resolve the module prefix, if any.
1340
        let module_prefix_result = self.resolve_module_prefix(module_, module_path);
1341

1342 1343
        let search_module;
        let start_index;
1344
        let last_private;
1345
        match module_prefix_result {
1346
            Failed(None) => {
1347
                let mpath = names_to_string(module_path);
1348
                let mpath = &mpath[..];
1349
                match mpath.rfind(':') {
C
Fix ICE  
Corey Richardson 已提交
1350
                    Some(idx) => {
1351
                        let msg = format!("Could not find `{}` in `{}`",
C
corentih 已提交
1352 1353 1354 1355
                                          // idx +- 1 to account for the
                                          // colons on either side
                                          &mpath[idx + 1..],
                                          &mpath[..idx - 1]);
1356
                        return Failed(Some((span, msg)));
C
corentih 已提交
1357
                    }
1358
                    None => {
C
corentih 已提交
1359
                        return Failed(None);
1360
                    }
1361
                }
1362
            }
1363
            Failed(err) => return Failed(err),
B
Brian Anderson 已提交
1364
            Indeterminate => {
C
corentih 已提交
1365
                debug!("(resolving module path for import) indeterminate; bailing");
B
Brian Anderson 已提交
1366
                return Indeterminate;
1367
            }
1368 1369 1370 1371 1372 1373 1374 1375
            Success(NoPrefixFound) => {
                // There was no prefix, so we're considering the first element
                // of the path. How we handle this depends on whether we were
                // instructed to use lexical scope or not.
                match use_lexical_scope {
                    DontUseLexicalScope => {
                        // This is a crate-relative path. We will start the
                        // resolution process at index zero.
1376
                        search_module = self.graph_root;
1377
                        start_index = 0;
1378
                        last_private = LastMod(AllPublic);
1379 1380 1381 1382 1383
                    }
                    UseLexicalScope => {
                        // This is not a crate-relative path. We resolve the
                        // first component of the path in the current lexical
                        // scope and then proceed to resolve below that.
1384 1385 1386 1387
                        match self.resolve_item_in_lexical_scope(module_,
                                                                 module_path[0],
                                                                 TypeNS,
                                                                 true) {
1388
                            Failed(err) => return Failed(err),
1389
                            Indeterminate => {
C
corentih 已提交
1390
                                debug!("(resolving module path for import) indeterminate; bailing");
1391 1392
                                return Indeterminate;
                            }
J
Jeffrey Seyfried 已提交
1393
                            Success((binding, _)) => match binding.module() {
1394 1395 1396 1397 1398 1399
                                Some(containing_module) => {
                                    search_module = containing_module;
                                    start_index = 1;
                                    last_private = LastMod(AllPublic);
                                }
                                None => return Failed(None),
1400 1401 1402 1403 1404
                            }
                        }
                    }
                }
            }
E
Eduard Burtescu 已提交
1405
            Success(PrefixFound(ref containing_module, index)) => {
1406
                search_module = containing_module;
1407
                start_index = index;
1408
                last_private = LastMod(DependsOn(containing_module.def_id()
1409
                                                                  .unwrap()));
1410 1411 1412
            }
        }

1413 1414 1415 1416
        self.resolve_module_path_from_root(search_module,
                                           module_path,
                                           start_index,
                                           span,
1417
                                           last_private)
1418 1419
    }

1420 1421
    /// Invariant: This must only be called during main resolution, not during
    /// import resolution.
F
Felix S. Klock II 已提交
1422
    fn resolve_item_in_lexical_scope(&mut self,
1423
                                     module_: Module<'a>,
1424
                                     name: Name,
1425 1426
                                     namespace: Namespace,
                                     record_used: bool)
J
Jeffrey Seyfried 已提交
1427
                                     -> ResolveResult<(NameBinding<'a>, bool)> {
C
corentih 已提交
1428
        debug!("(resolving item in lexical scope) resolving `{}` in namespace {:?} in `{}`",
1429
               name,
1430
               namespace,
1431
               module_to_string(&*module_));
1432

1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
        // Proceed up the scope chain looking for parent modules.
        let mut search_module = module_;
        loop {
            // Resolve the name in the parent module.
            match self.resolve_name_in_module(search_module, name, namespace, true, record_used) {
                Failed(Some((span, msg))) => {
                    resolve_error(self, span, ResolutionError::FailedToResolve(&*msg));
                }
                Failed(None) => (), // Continue up the search chain.
                Indeterminate => {
                    // We couldn't see through the higher scope because of an
                    // unresolved import higher up. Bail.
1445

1446 1447
                    debug!("(resolving item in lexical scope) indeterminate higher scope; bailing");
                    return Indeterminate;
1448
                }
J
Jeffrey Seyfried 已提交
1449
                Success((binding, used_reexport)) => {
1450 1451
                    // We found the module.
                    debug!("(resolving item in lexical scope) found name in module, done");
J
Jeffrey Seyfried 已提交
1452
                    return Success((binding, used_reexport));
1453 1454 1455 1456
                }
            }

            // Go to the next parent.
1457
            match search_module.parent_link {
B
Brian Anderson 已提交
1458
                NoParentLink => {
1459
                    // No more parents. This module was unresolved.
1460
                    debug!("(resolving item in lexical scope) unresolved module: no parent module");
1461
                    return Failed(None);
1462
                }
1463
                ModuleParentLink(parent_module_node, _) => {
1464 1465 1466 1467
                    if search_module.is_normal() {
                        // We stop the search here.
                        debug!("(resolving item in lexical scope) unresolved module: not \
                                searching through module parents");
1468
                            return Failed(None);
1469
                    } else {
1470
                        search_module = parent_module_node;
1471 1472
                    }
                }
1473 1474
                BlockParentLink(parent_module_node, _) => {
                    search_module = parent_module_node;
1475 1476 1477 1478 1479
                }
            }
        }
    }

1480
    /// Returns the nearest normal module parent of the given module.
1481
    fn get_nearest_normal_module_parent(&mut self, module_: Module<'a>) -> Option<Module<'a>> {
1482 1483
        let mut module_ = module_;
        loop {
1484
            match module_.parent_link {
1485 1486 1487
                NoParentLink => return None,
                ModuleParentLink(new_module, _) |
                BlockParentLink(new_module, _) => {
1488
                    let new_module = new_module;
1489 1490
                    if new_module.is_normal() {
                        return Some(new_module);
1491
                    }
1492
                    module_ = new_module;
1493 1494 1495 1496 1497
                }
            }
        }
    }

1498 1499
    /// Returns the nearest normal module parent of the given module, or the
    /// module itself if it is a normal module.
1500
    fn get_nearest_normal_module_parent_or_self(&mut self, module_: Module<'a>) -> Module<'a> {
1501 1502 1503
        if module_.is_normal() {
            return module_;
        }
1504
        match self.get_nearest_normal_module_parent(module_) {
1505 1506
            None => module_,
            Some(new_module) => new_module,
1507 1508 1509
        }
    }

1510
    /// Resolves a "module prefix". A module prefix is one or both of (a) `self::`;
1511
    /// (b) some chain of `super::`.
1512
    /// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) *
F
Felix S. Klock II 已提交
1513
    fn resolve_module_prefix(&mut self,
1514
                             module_: Module<'a>,
1515
                             module_path: &[Name])
1516
                             -> ResolveResult<ModulePrefixResult<'a>> {
1517 1518
        // Start at the current module if we see `self` or `super`, or at the
        // top of the crate otherwise.
1519 1520 1521 1522 1523 1524
        let mut i = match &*module_path[0].as_str() {
            "self" => 1,
            "super" => 0,
            _ => return Success(NoPrefixFound),
        };
        let mut containing_module = self.get_nearest_normal_module_parent_or_self(module_);
1525 1526

        // Now loop through all the `super`s we find.
1527
        while i < module_path.len() && "super" == module_path[i].as_str() {
1528
            debug!("(resolving module prefix) resolving `super` at {}",
1529
                   module_to_string(&*containing_module));
1530
            match self.get_nearest_normal_module_parent(containing_module) {
1531
                None => return Failed(None),
1532 1533 1534
                Some(new_module) => {
                    containing_module = new_module;
                    i += 1;
1535 1536 1537 1538
                }
            }
        }

1539
        debug!("(resolving module prefix) finished resolving prefix at {}",
1540
               module_to_string(&*containing_module));
1541 1542

        return Success(PrefixFound(containing_module, i));
1543 1544
    }

1545
    /// Attempts to resolve the supplied name in the given module for the
J
Jeffrey Seyfried 已提交
1546
    /// given namespace. If successful, returns the binding corresponding to
1547
    /// the name.
1548 1549 1550
    ///
    /// The boolean returned on success is an indicator of whether this lookup
    /// passed through a public re-export proxy.
F
Felix S. Klock II 已提交
1551
    fn resolve_name_in_module(&mut self,
1552
                              module_: Module<'a>,
1553
                              name: Name,
1554
                              namespace: Namespace,
1555 1556
                              allow_private_imports: bool,
                              record_used: bool)
J
Jeffrey Seyfried 已提交
1557
                              -> ResolveResult<(NameBinding<'a>, bool)> {
1558
        debug!("(resolving name in module) resolving `{}` in `{}`",
1559
               name,
1560
               module_to_string(&*module_));
1561 1562

        // First, check the direct children of the module.
1563
        build_reduced_graph::populate_module_if_necessary(self, module_);
1564

1565 1566
        if let Some(binding) = module_.get_child(name, namespace) {
            debug!("(resolving name in module) found node as child");
1567 1568 1569 1570 1571 1572
            if binding.is_extern_crate() {
                // track the extern crate as used.
                if let Some(DefId { krate, .. }) = binding.module().unwrap().def_id() {
                    self.used_crates.insert(krate);
                }
            }
J
Jeffrey Seyfried 已提交
1573
            return Success((binding, false));
1574 1575
        }

1576
        // Check the list of resolved imports.
1577 1578 1579
        match module_.import_resolutions.borrow().get(&(name, namespace)) {
            Some(import_resolution) if allow_private_imports || import_resolution.is_public => {
                if import_resolution.is_public && import_resolution.outstanding_references != 0 {
C
corentih 已提交
1580
                    debug!("(resolving name in module) import unresolved; bailing out");
B
Brian Anderson 已提交
1581
                    return Indeterminate;
1582
                }
J
Jeffrey Seyfried 已提交
1583
                if let Some(binding) = import_resolution.binding.clone() {
1584 1585 1586
                    debug!("(resolving name in module) resolved to import");
                    if record_used {
                        self.record_import_use(name, namespace, &import_resolution);
1587
                    }
J
Jeffrey Seyfried 已提交
1588
                    return Success((binding, true));
1589 1590
                }
            }
1591
            Some(..) | None => {} // Continue.
1592 1593
        }

1594
        // We're out of luck.
C
corentih 已提交
1595
        debug!("(resolving name in module) failed to resolve `{}`", name);
1596
        return Failed(None);
1597 1598
    }

1599
    fn report_unresolved_imports(&mut self, module_: Module<'a>) {
1600
        let index = module_.resolved_import_count.get();
1601 1602
        let imports = module_.imports.borrow();
        let import_count = imports.len();
1603
        if index != import_count {
1604 1605 1606
            resolve_error(self,
                          (*imports)[index].span,
                          ResolutionError::UnresolvedImport(None));
1607 1608 1609
        }

        // Descend into children and anonymous children.
1610
        build_reduced_graph::populate_module_if_necessary(self, module_);
1611

1612
        module_.for_each_local_child(|_, _, child_node| {
1613
            match child_node.module() {
1614 1615 1616 1617 1618
                None => {
                    // Continue.
                }
                Some(child_module) => {
                    self.report_unresolved_imports(child_module);
1619 1620
                }
            }
1621
        });
1622

1623
        for (_, module_) in module_.anonymous_children.borrow().iter() {
1624
            self.report_unresolved_imports(module_);
1625 1626 1627 1628 1629
        }
    }

    // AST resolution
    //
1630
    // We maintain a list of value ribs and type ribs.
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
    //
    // Simultaneously, we keep track of the current position in the module
    // graph in the `current_module` pointer. When we go to resolve a name in
    // the value or type namespaces, we first look through all the ribs and
    // then query the module graph. When we resolve a name in the module
    // namespace, we can skip all the ribs (since nested modules are not
    // allowed within blocks in Rust) and jump straight to the current module
    // graph node.
    //
    // Named implementations are handled separately. When we find a method
    // call, we consult the module node to find all of the implementations in
    // scope. This information is lazily cached in the module node. We then
    // generate a fake "implementation scope" containing all the
    // implementations thus found, for compatibility with old resolve pass.

C
corentih 已提交
1646 1647
    fn with_scope<F>(&mut self, name: Option<Name>, f: F)
        where F: FnOnce(&mut Resolver)
J
Jorge Aparicio 已提交
1648
    {
1649
        let orig_module = self.current_module;
1650 1651

        // Move down in the graph.
1652
        match name {
B
Brian Anderson 已提交
1653
            None => {
1654 1655
                // Nothing to do.
            }
B
Brian Anderson 已提交
1656
            Some(name) => {
1657
                build_reduced_graph::populate_module_if_necessary(self, &orig_module);
1658

1659
                match orig_module.get_child(name, TypeNS) {
B
Brian Anderson 已提交
1660
                    None => {
1661
                        debug!("!!! (with scope) didn't find `{}` in `{}`",
1662
                               name,
1663
                               module_to_string(&*orig_module));
1664
                    }
1665 1666
                    Some(name_binding) => {
                        match name_binding.module() {
B
Brian Anderson 已提交
1667
                            None => {
C
corentih 已提交
1668
                                debug!("!!! (with scope) didn't find module for `{}` in `{}`",
1669
                                       name,
1670
                                       module_to_string(&*orig_module));
1671
                            }
B
Brian Anderson 已提交
1672
                            Some(module_) => {
1673
                                self.current_module = module_;
1674 1675 1676 1677 1678 1679 1680
                            }
                        }
                    }
                }
            }
        }

A
Alex Crichton 已提交
1681
        f(self);
1682 1683 1684 1685

        self.current_module = orig_module;
    }

S
Seo Sanghyeon 已提交
1686 1687
    /// Searches the current set of local scopes for labels.
    /// Stops after meeting a closure.
1688 1689 1690 1691 1692 1693 1694 1695
    fn search_label(&self, name: Name) -> Option<DefLike> {
        for rib in self.label_ribs.iter().rev() {
            match rib.kind {
                NormalRibKind => {
                    // Continue
                }
                _ => {
                    // Do not resolve labels across function boundary
C
corentih 已提交
1696
                    return None;
1697 1698 1699
                }
            }
            let result = rib.bindings.get(&name).cloned();
S
Seo Sanghyeon 已提交
1700
            if result.is_some() {
C
corentih 已提交
1701
                return result;
1702 1703 1704 1705 1706
            }
        }
        None
    }

1707
    fn resolve_crate(&mut self, krate: &hir::Crate) {
1708
        debug!("(resolving crate) starting");
1709

1710
        intravisit::walk_crate(self, krate);
1711 1712
    }

W
we 已提交
1713 1714
    fn check_if_primitive_type_name(&self, name: Name, span: Span) {
        if let Some(_) = self.primitive_type_table.primitive_types.get(&name) {
C
corentih 已提交
1715 1716 1717 1718
            span_err!(self.session,
                      span,
                      E0317,
                      "user-defined types or type parameters cannot shadow the primitive types");
W
we 已提交
1719 1720 1721
        }
    }

1722
    fn resolve_item(&mut self, item: &Item) {
V
Vadim Petrochenkov 已提交
1723
        let name = item.name;
1724

C
corentih 已提交
1725
        debug!("(resolving item) resolving {}", name);
1726

1727
        match item.node {
1728 1729 1730
            ItemEnum(_, ref generics) |
            ItemTy(_, ref generics) |
            ItemStruct(_, ref generics) => {
W
we 已提交
1731 1732
                self.check_if_primitive_type_name(name, item.span);

C
corentih 已提交
1733
                self.with_type_parameter_rib(HasTypeParameters(generics, TypeSpace, ItemRibKind),
1734
                                             |this| intravisit::walk_item(this, item));
1735
            }
1736
            ItemFn(_, _, _, _, ref generics, _) => {
C
corentih 已提交
1737
                self.with_type_parameter_rib(HasTypeParameters(generics, FnSpace, ItemRibKind),
1738
                                             |this| intravisit::walk_item(this, item));
1739 1740
            }

F
Flavio Percoco 已提交
1741
            ItemDefaultImpl(_, ref trait_ref) => {
1742
                self.with_optional_trait_ref(Some(trait_ref), |_, _| {});
1743
            }
C
corentih 已提交
1744
            ItemImpl(_, _, ref generics, ref opt_trait_ref, ref self_type, ref impl_items) => {
1745
                self.resolve_implementation(generics,
1746
                                            opt_trait_ref,
1747
                                            &**self_type,
1748
                                            item.id,
1749
                                            impl_items);
1750 1751
            }

N
Nick Cameron 已提交
1752
            ItemTrait(_, ref generics, ref bounds, ref trait_items) => {
W
we 已提交
1753 1754
                self.check_if_primitive_type_name(name, item.span);

1755 1756 1757 1758 1759
                // Create a new rib for the trait-wide type parameters.
                self.with_type_parameter_rib(HasTypeParameters(generics,
                                                               TypeSpace,
                                                               ItemRibKind),
                                             |this| {
1760
                    let local_def_id = this.ast_map.local_def_id(item.id);
1761
                    this.with_self_rib(Def::SelfTy(Some(local_def_id), None), |this| {
1762
                        this.visit_generics(generics);
1763
                        walk_list!(this, visit_ty_param_bound, bounds);
1764 1765

                        for trait_item in trait_items {
1766
                            match trait_item.node {
1767
                                hir::ConstTraitItem(_, ref default) => {
1768 1769 1770 1771 1772
                                    // Only impose the restrictions of
                                    // ConstRibKind if there's an actual constant
                                    // expression in a provided default.
                                    if default.is_some() {
                                        this.with_constant_rib(|this| {
1773
                                            intravisit::walk_trait_item(this, trait_item)
1774 1775
                                        });
                                    } else {
1776
                                        intravisit::walk_trait_item(this, trait_item)
1777 1778
                                    }
                                }
1779
                                hir::MethodTraitItem(ref sig, _) => {
1780 1781 1782 1783 1784
                                    let type_parameters =
                                        HasTypeParameters(&sig.generics,
                                                          FnSpace,
                                                          MethodRibKind);
                                    this.with_type_parameter_rib(type_parameters, |this| {
1785
                                        intravisit::walk_trait_item(this, trait_item)
1786
                                    });
1787
                                }
1788
                                hir::TypeTraitItem(..) => {
V
Vadim Petrochenkov 已提交
1789
                                    this.check_if_primitive_type_name(trait_item.name,
1790
                                                                      trait_item.span);
1791
                                    this.with_type_parameter_rib(NoTypeParameters, |this| {
1792
                                        intravisit::walk_trait_item(this, trait_item)
1793
                                    });
1794 1795 1796 1797
                                }
                            };
                        }
                    });
1798
                });
1799 1800
            }

1801
            ItemMod(_) | ItemForeignMod(_) => {
1802
                self.with_scope(Some(name), |this| {
1803
                    intravisit::walk_item(this, item);
1804
                });
1805 1806
            }

1807
            ItemConst(..) | ItemStatic(..) => {
A
Alex Crichton 已提交
1808
                self.with_constant_rib(|this| {
1809
                    intravisit::walk_item(this, item);
1810
                });
1811
            }
1812

W
we 已提交
1813 1814
            ItemUse(ref view_path) => {
                // check for imports shadowing primitive types
1815
                let check_rename = |this: &Self, id, name| {
1816
                    match this.def_map.borrow().get(&id).map(|d| d.full_def()) {
1817 1818
                        Some(Def::Enum(..)) | Some(Def::TyAlias(..)) | Some(Def::Struct(..)) |
                        Some(Def::Trait(..)) | None => {
1819
                            this.check_if_primitive_type_name(name, item.span);
W
we 已提交
1820 1821 1822
                        }
                        _ => {}
                    }
1823 1824 1825
                };

                match view_path.node {
1826 1827
                    hir::ViewPathSimple(name, _) => {
                        check_rename(self, item.id, name);
1828
                    }
1829
                    hir::ViewPathList(ref prefix, ref items) => {
1830
                        for item in items {
1831 1832
                            if let Some(name) = item.node.rename() {
                                check_rename(self, item.node.id(), name);
1833 1834 1835 1836 1837 1838 1839 1840
                            }
                        }

                        // Resolve prefix of an import with empty braces (issue #28388)
                        if items.is_empty() && !prefix.segments.is_empty() {
                            match self.resolve_crate_relative_path(prefix.span,
                                                                   &prefix.segments,
                                                                   TypeNS) {
C
corentih 已提交
1841 1842
                                Some((def, lp)) =>
                                    self.record_def(item.id, PathResolution::new(def, lp, 0)),
1843 1844 1845 1846 1847
                                None => {
                                    resolve_error(self,
                                                  prefix.span,
                                                  ResolutionError::FailedToResolve(
                                                      &path_names_to_string(prefix, 0)));
1848
                                    self.record_def(item.id, err_path_resolution());
1849
                                }
1850 1851 1852 1853
                            }
                        }
                    }
                    _ => {}
W
we 已提交
1854 1855 1856
                }
            }

1857
            ItemExternCrate(_) => {
1858
                // do nothing, these are just around to be encoded
1859
            }
1860 1861 1862
        }
    }

1863
    fn with_type_parameter_rib<'b, F>(&'b mut self, type_parameters: TypeParameters<'a, 'b>, f: F)
C
corentih 已提交
1864
        where F: FnOnce(&mut Resolver)
J
Jorge Aparicio 已提交
1865
    {
1866
        match type_parameters {
1867
            HasTypeParameters(generics, space, rib_kind) => {
1868
                let mut function_type_rib = Rib::new(rib_kind);
1869
                let mut seen_bindings = HashSet::new();
D
Daniel Micay 已提交
1870
                for (index, type_parameter) in generics.ty_params.iter().enumerate() {
1871
                    let name = type_parameter.name;
1872
                    debug!("with_type_parameter_rib: {}", type_parameter.id);
1873

1874
                    if seen_bindings.contains(&name) {
1875 1876
                        resolve_error(self,
                                      type_parameter.span,
C
corentih 已提交
1877
                                      ResolutionError::NameAlreadyUsedInTypeParameterList(name));
1878
                    }
1879
                    seen_bindings.insert(name);
1880

1881
                    // plain insert (no renaming)
C
corentih 已提交
1882 1883
                    function_type_rib.bindings
                                     .insert(name,
1884
                                             DlDef(Def::TyParam(space,
C
corentih 已提交
1885 1886 1887 1888
                                                              index as u32,
                                                              self.ast_map
                                                                  .local_def_id(type_parameter.id),
                                                              name)));
1889
                }
1890
                self.type_ribs.push(function_type_rib);
1891 1892
            }

B
Brian Anderson 已提交
1893
            NoTypeParameters => {
1894 1895 1896 1897
                // Nothing to do.
            }
        }

A
Alex Crichton 已提交
1898
        f(self);
1899

1900
        match type_parameters {
C
corentih 已提交
1901 1902 1903 1904 1905 1906
            HasTypeParameters(..) => {
                if !self.resolved {
                    self.type_ribs.pop();
                }
            }
            NoTypeParameters => {}
1907 1908 1909
        }
    }

C
corentih 已提交
1910 1911
    fn with_label_rib<F>(&mut self, f: F)
        where F: FnOnce(&mut Resolver)
J
Jorge Aparicio 已提交
1912
    {
1913
        self.label_ribs.push(Rib::new(NormalRibKind));
A
Alex Crichton 已提交
1914
        f(self);
G
Garming Sam 已提交
1915 1916 1917
        if !self.resolved {
            self.label_ribs.pop();
        }
1918
    }
1919

C
corentih 已提交
1920 1921
    fn with_constant_rib<F>(&mut self, f: F)
        where F: FnOnce(&mut Resolver)
J
Jorge Aparicio 已提交
1922
    {
1923 1924
        self.value_ribs.push(Rib::new(ConstantItemRibKind));
        self.type_ribs.push(Rib::new(ConstantItemRibKind));
A
Alex Crichton 已提交
1925
        f(self);
G
Garming Sam 已提交
1926 1927 1928 1929
        if !self.resolved {
            self.type_ribs.pop();
            self.value_ribs.pop();
        }
1930 1931
    }

1932
    fn resolve_function(&mut self, rib_kind: RibKind<'a>, declaration: &FnDecl, block: &Block) {
1933
        // Create a value rib for the function.
1934
        self.value_ribs.push(Rib::new(rib_kind));
1935

1936
        // Create a label rib for the function.
1937
        self.label_ribs.push(Rib::new(rib_kind));
1938

1939 1940 1941
        // Add each argument to the rib.
        let mut bindings_list = HashMap::new();
        for argument in &declaration.inputs {
C
corentih 已提交
1942
            self.resolve_pattern(&*argument.pat, ArgumentIrrefutableMode, &mut bindings_list);
1943

1944
            self.visit_ty(&*argument.ty);
1945

1946 1947
            debug!("(resolving function) recorded argument");
        }
1948
        intravisit::walk_fn_ret_ty(self, &declaration.output);
1949

1950
        // Resolve the function body.
1951
        self.visit_block(block);
1952

1953
        debug!("(resolving function) leaving function");
1954

G
Garming Sam 已提交
1955 1956 1957 1958
        if !self.resolved {
            self.label_ribs.pop();
            self.value_ribs.pop();
        }
1959 1960
    }

F
Felix S. Klock II 已提交
1961
    fn resolve_trait_reference(&mut self,
N
Nick Cameron 已提交
1962
                               id: NodeId,
1963
                               trait_path: &Path,
1964
                               path_depth: usize)
1965 1966
                               -> Result<PathResolution, ()> {
        if let Some(path_res) = self.resolve_path(id, trait_path, path_depth, TypeNS, true) {
1967
            if let Def::Trait(_) = path_res.base_def {
1968 1969 1970
                debug!("(resolving trait) found trait def: {:?}", path_res);
                Ok(path_res)
            } else {
N
Nick Cameron 已提交
1971 1972 1973 1974 1975
                let mut err =
                    resolve_struct_error(self,
                                  trait_path.span,
                                  ResolutionError::IsNotATrait(&*path_names_to_string(trait_path,
                                                                                      path_depth)));
1976 1977

                // If it's a typedef, give a note
1978
                if let Def::TyAlias(..) = path_res.base_def {
N
Nick Cameron 已提交
1979 1980
                    err.span_note(trait_path.span,
                                  "`type` aliases cannot be used for traits");
1981
                }
N
Nick Cameron 已提交
1982
                err.emit();
1983 1984
                Err(())
            }
1985
        } else {
1986 1987
            resolve_error(self,
                          trait_path.span,
C
corentih 已提交
1988 1989
                          ResolutionError::UndeclaredTraitName(&*path_names_to_string(trait_path,
                                                                                      path_depth)));
1990
            Err(())
1991 1992 1993
        }
    }

1994
    fn resolve_generics(&mut self, generics: &Generics) {
1995
        for type_parameter in generics.ty_params.iter() {
1996
            self.check_if_primitive_type_name(type_parameter.name, type_parameter.span);
1997 1998
        }
        for predicate in &generics.where_clause.predicates {
1999
            match predicate {
2000 2001 2002
                &hir::WherePredicate::BoundPredicate(_) |
                &hir::WherePredicate::RegionPredicate(_) => {}
                &hir::WherePredicate::EqPredicate(ref eq_pred) => {
2003
                    let path_res = self.resolve_path(eq_pred.id, &eq_pred.path, 0, TypeNS, true);
2004
                    if let Some(PathResolution { base_def: Def::TyParam(..), .. }) = path_res {
2005 2006
                        self.record_def(eq_pred.id, path_res.unwrap());
                    } else {
2007 2008
                        resolve_error(self,
                                      eq_pred.span,
2009
                                      ResolutionError::UndeclaredAssociatedType);
2010
                        self.record_def(eq_pred.id, err_path_resolution());
2011 2012
                    }
                }
2013 2014
            }
        }
2015
        intravisit::walk_generics(self, generics);
2016 2017
    }

2018 2019
    fn with_current_self_type<T, F>(&mut self, self_type: &Ty, f: F) -> T
        where F: FnOnce(&mut Resolver) -> T
J
Jorge Aparicio 已提交
2020
    {
2021 2022 2023 2024 2025 2026 2027
        // Handle nested impls (inside fn bodies)
        let previous_value = replace(&mut self.current_self_type, Some(self_type.clone()));
        let result = f(self);
        self.current_self_type = previous_value;
        result
    }

C
corentih 已提交
2028
    fn with_optional_trait_ref<T, F>(&mut self, opt_trait_ref: Option<&TraitRef>, f: F) -> T
2029
        where F: FnOnce(&mut Resolver, Option<DefId>) -> T
J
Jorge Aparicio 已提交
2030
    {
2031
        let mut new_val = None;
2032
        let mut new_id = None;
E
Eduard Burtescu 已提交
2033
        if let Some(trait_ref) = opt_trait_ref {
2034
            if let Ok(path_res) = self.resolve_trait_reference(trait_ref.ref_id,
C
corentih 已提交
2035 2036
                                                               &trait_ref.path,
                                                               0) {
2037 2038 2039 2040
                assert!(path_res.depth == 0);
                self.record_def(trait_ref.ref_id, path_res);
                new_val = Some((path_res.base_def.def_id(), trait_ref.clone()));
                new_id = Some(path_res.base_def.def_id());
2041 2042
            } else {
                self.record_def(trait_ref.ref_id, err_path_resolution());
2043
            }
2044
            intravisit::walk_trait_ref(self, trait_ref);
2045
        }
2046
        let original_trait_ref = replace(&mut self.current_trait_ref, new_val);
2047
        let result = f(self, new_id);
2048 2049 2050 2051
        self.current_trait_ref = original_trait_ref;
        result
    }

2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
    fn with_self_rib<F>(&mut self, self_def: Def, f: F)
        where F: FnOnce(&mut Resolver)
    {
        let mut self_type_rib = Rib::new(NormalRibKind);

        // plain insert (no renaming, types are not currently hygienic....)
        let name = special_names::type_self;
        self_type_rib.bindings.insert(name, DlDef(self_def));
        self.type_ribs.push(self_type_rib);
        f(self);
G
Garming Sam 已提交
2062 2063 2064
        if !self.resolved {
            self.type_ribs.pop();
        }
2065 2066
    }

F
Felix S. Klock II 已提交
2067
    fn resolve_implementation(&mut self,
2068 2069 2070
                              generics: &Generics,
                              opt_trait_reference: &Option<TraitRef>,
                              self_type: &Ty,
2071
                              item_id: NodeId,
2072
                              impl_items: &[ImplItem]) {
2073
        // If applicable, create a rib for the type parameters.
2074
        self.with_type_parameter_rib(HasTypeParameters(generics,
2075
                                                       TypeSpace,
2076
                                                       ItemRibKind),
2077
                                     |this| {
2078
            // Resolve the type parameters.
2079
            this.visit_generics(generics);
2080

2081
            // Resolve the trait reference, if necessary.
2082
            this.with_optional_trait_ref(opt_trait_reference.as_ref(), |this, trait_id| {
2083
                // Resolve the self type.
2084
                this.visit_ty(self_type);
2085

2086
                this.with_self_rib(Def::SelfTy(trait_id, Some((item_id, self_type.id))), |this| {
2087 2088 2089
                    this.with_current_self_type(self_type, |this| {
                        for impl_item in impl_items {
                            match impl_item.node {
2090
                                hir::ImplItemKind::Const(..) => {
2091
                                    // If this is a trait impl, ensure the const
2092
                                    // exists in trait
V
Vadim Petrochenkov 已提交
2093
                                    this.check_trait_item(impl_item.name,
2094 2095
                                                          impl_item.span,
                                        |n, s| ResolutionError::ConstNotMemberOfTrait(n, s));
2096
                                    this.with_constant_rib(|this| {
2097
                                        intravisit::walk_impl_item(this, impl_item);
2098 2099
                                    });
                                }
2100
                                hir::ImplItemKind::Method(ref sig, _) => {
2101 2102
                                    // If this is a trait impl, ensure the method
                                    // exists in trait
V
Vadim Petrochenkov 已提交
2103
                                    this.check_trait_item(impl_item.name,
2104 2105
                                                          impl_item.span,
                                        |n, s| ResolutionError::MethodNotMemberOfTrait(n, s));
2106 2107 2108 2109 2110 2111 2112 2113

                                    // We also need a new scope for the method-
                                    // specific type parameters.
                                    let type_parameters =
                                        HasTypeParameters(&sig.generics,
                                                          FnSpace,
                                                          MethodRibKind);
                                    this.with_type_parameter_rib(type_parameters, |this| {
2114
                                        intravisit::walk_impl_item(this, impl_item);
2115 2116
                                    });
                                }
2117
                                hir::ImplItemKind::Type(ref ty) => {
2118
                                    // If this is a trait impl, ensure the type
2119
                                    // exists in trait
V
Vadim Petrochenkov 已提交
2120
                                    this.check_trait_item(impl_item.name,
2121 2122
                                                          impl_item.span,
                                        |n, s| ResolutionError::TypeNotMemberOfTrait(n, s));
2123

2124 2125
                                    this.visit_ty(ty);
                                }
2126
                            }
2127
                        }
2128
                    });
2129 2130
                });
            });
2131
        });
2132 2133
    }

2134
    fn check_trait_item<F>(&self, name: Name, span: Span, err: F)
C
corentih 已提交
2135 2136 2137 2138
        where F: FnOnce(Name, &str) -> ResolutionError
    {
        // If there is a TraitRef in scope for an impl, then the method must be in the
        // trait.
2139
        if let Some((did, ref trait_ref)) = self.current_trait_ref {
2140
            if !self.trait_item_map.contains_key(&(name, did)) {
2141
                let path_str = path_names_to_string(&trait_ref.path, 0);
C
corentih 已提交
2142
                resolve_error(self, span, err(name, &*path_str));
2143 2144 2145 2146
            }
        }
    }

E
Eduard Burtescu 已提交
2147
    fn resolve_local(&mut self, local: &Local) {
2148
        // Resolve the type.
2149
        walk_list!(self, visit_ty, &local.ty);
2150

2151
        // Resolve the initializer.
2152
        walk_list!(self, visit_expr, &local.init);
2153 2154

        // Resolve the pattern.
C
corentih 已提交
2155
        self.resolve_pattern(&*local.pat, LocalIrrefutableMode, &mut HashMap::new());
2156 2157
    }

J
John Clements 已提交
2158 2159 2160 2161
    // build a map from pattern identifiers to binding-info's.
    // this is done hygienically. This could arise for a macro
    // that expands into an or-pattern where one 'x' was from the
    // user and one 'x' came from the macro.
E
Eduard Burtescu 已提交
2162
    fn binding_mode_map(&mut self, pat: &Pat) -> BindingMap {
2163
        let mut result = HashMap::new();
2164 2165
        pat_bindings(&self.def_map, pat, |binding_mode, _id, sp, path1| {
            let name = path1.node;
C
corentih 已提交
2166 2167 2168 2169 2170
            result.insert(name,
                          BindingInfo {
                              span: sp,
                              binding_mode: binding_mode,
                          });
2171
        });
2172
        return result;
2173 2174
    }

J
John Clements 已提交
2175 2176
    // check that all of the arms in an or-pattern have exactly the
    // same set of bindings, with the same binding modes for each.
F
Felix S. Klock II 已提交
2177
    fn check_consistent_bindings(&mut self, arm: &Arm) {
2178
        if arm.pats.is_empty() {
C
corentih 已提交
2179
            return;
2180
        }
2181
        let map_0 = self.binding_mode_map(&*arm.pats[0]);
D
Daniel Micay 已提交
2182
        for (i, p) in arm.pats.iter().enumerate() {
2183
            let map_i = self.binding_mode_map(&**p);
2184

2185
            for (&key, &binding_0) in &map_0 {
2186
                match map_i.get(&key) {
C
corentih 已提交
2187
                    None => {
2188
                        resolve_error(self,
C
corentih 已提交
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
                                      p.span,
                                      ResolutionError::VariableNotBoundInPattern(key, i + 1));
                    }
                    Some(binding_i) => {
                        if binding_0.binding_mode != binding_i.binding_mode {
                            resolve_error(self,
                                          binding_i.span,
                                          ResolutionError::VariableBoundWithDifferentMode(key,
                                                                                          i + 1));
                        }
2199
                    }
2200 2201 2202
                }
            }

2203
            for (&key, &binding) in &map_i {
2204
                if !map_0.contains_key(&key) {
2205 2206
                    resolve_error(self,
                                  binding.span,
C
corentih 已提交
2207
                                  ResolutionError::VariableNotBoundInParentPattern(key, i + 1));
2208 2209 2210
                }
            }
        }
2211 2212
    }

F
Felix S. Klock II 已提交
2213
    fn resolve_arm(&mut self, arm: &Arm) {
2214
        self.value_ribs.push(Rib::new(NormalRibKind));
2215

2216
        let mut bindings_list = HashMap::new();
2217
        for pattern in &arm.pats {
2218
            self.resolve_pattern(&**pattern, RefutableMode, &mut bindings_list);
2219 2220
        }

2221 2222 2223 2224
        // This has to happen *after* we determine which
        // pat_idents are variants
        self.check_consistent_bindings(arm);

2225
        walk_list!(self, visit_expr, &arm.guard);
2226
        self.visit_expr(&*arm.body);
2227

G
Garming Sam 已提交
2228 2229 2230
        if !self.resolved {
            self.value_ribs.pop();
        }
2231 2232
    }

E
Eduard Burtescu 已提交
2233
    fn resolve_block(&mut self, block: &Block) {
2234
        debug!("(resolving block) entering block");
2235
        // Move down in the graph, if there's an anonymous module rooted here.
2236
        let orig_module = self.current_module;
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
        let anonymous_module =
            orig_module.anonymous_children.borrow().get(&block.id).map(|module| *module);

        if let Some(anonymous_module) = anonymous_module {
            debug!("(resolving block) found anonymous module, moving down");
            self.value_ribs.push(Rib::new(AnonymousModuleRibKind(anonymous_module)));
            self.type_ribs.push(Rib::new(AnonymousModuleRibKind(anonymous_module)));
            self.current_module = anonymous_module;
        } else {
            self.value_ribs.push(Rib::new(NormalRibKind));
2247 2248 2249
        }

        // Descend into the block.
2250
        intravisit::walk_block(self, block);
2251 2252

        // Move back up.
G
Garming Sam 已提交
2253
        if !self.resolved {
2254
            self.current_module = orig_module;
G
Garming Sam 已提交
2255
            self.value_ribs.pop();
2256 2257 2258
            if let Some(_) = anonymous_module {
                self.type_ribs.pop();
            }
G
Garming Sam 已提交
2259
        }
2260
        debug!("(resolving block) leaving block");
2261 2262
    }

F
Felix S. Klock II 已提交
2263
    fn resolve_type(&mut self, ty: &Ty) {
2264
        match ty.node {
2265
            TyPath(ref maybe_qself, ref path) => {
C
corentih 已提交
2266 2267 2268 2269 2270 2271 2272 2273
                let resolution = match self.resolve_possibly_assoc_item(ty.id,
                                                                        maybe_qself.as_ref(),
                                                                        path,
                                                                        TypeNS,
                                                                        true) {
                    // `<T>::a::b::c` is resolved by typeck alone.
                    TypecheckRequired => {
                        // Resolve embedded types.
2274
                        intravisit::walk_ty(self, ty);
C
corentih 已提交
2275 2276 2277 2278
                        return;
                    }
                    ResolveAttempt(resolution) => resolution,
                };
2279 2280

                // This is a path in the type namespace. Walk through scopes
2281
                // looking for it.
2282
                match resolution {
B
Brian Anderson 已提交
2283
                    Some(def) => {
2284
                        // Write the result into the def map.
C
corentih 已提交
2285
                        debug!("(resolving type) writing resolution for `{}` (id {}) = {:?}",
2286
                               path_names_to_string(path, 0),
C
corentih 已提交
2287 2288
                               ty.id,
                               def);
2289
                        self.record_def(ty.id, def);
2290
                    }
B
Brian Anderson 已提交
2291
                    None => {
2292 2293
                        self.record_def(ty.id, err_path_resolution());

2294 2295 2296
                        // Keep reporting some errors even if they're ignored above.
                        self.resolve_path(ty.id, path, 0, TypeNS, true);

2297 2298 2299 2300
                        let kind = if maybe_qself.is_some() {
                            "associated type"
                        } else {
                            "type name"
2301
                        };
2302

2303
                        let self_type_name = special_idents::type_self.name;
C
corentih 已提交
2304 2305 2306 2307
                        let is_invalid_self_type_name = path.segments.len() > 0 &&
                                                        maybe_qself.is_none() &&
                                                        path.segments[0].identifier.name ==
                                                        self_type_name;
G
Guillaume Gomez 已提交
2308
                        if is_invalid_self_type_name {
2309 2310
                            resolve_error(self,
                                          ty.span,
2311
                                          ResolutionError::SelfUsedOutsideImplOrTrait);
2312
                        } else {
2313 2314
                            resolve_error(self,
                                          ty.span,
2315
                                          ResolutionError::UseOfUndeclared(
2316 2317 2318 2319
                                                                    kind,
                                                                    &*path_names_to_string(path,
                                                                                           0))
                                         );
G
Guillaume Gomez 已提交
2320
                        }
2321 2322
                    }
                }
2323
            }
2324
            _ => {}
2325
        }
2326
        // Resolve embedded types.
2327
        intravisit::walk_ty(self, ty);
2328 2329
    }

F
Felix S. Klock II 已提交
2330
    fn resolve_pattern(&mut self,
E
Eduard Burtescu 已提交
2331
                       pattern: &Pat,
2332 2333 2334
                       mode: PatternBindingMode,
                       // Maps idents to the node ID for the (outermost)
                       // pattern that binds them
2335
                       bindings_list: &mut HashMap<Name, NodeId>) {
2336
        let pat_id = pattern.id;
2337
        walk_pat(pattern, |pattern| {
2338
            match pattern.node {
2339 2340
                PatIdent(binding_mode, ref path1, ref at_rhs) => {
                    // The meaning of PatIdent with no type parameters
2341 2342 2343 2344
                    // depends on whether an enum variant or unit-like struct
                    // with that name is in scope. The probing lookup has to
                    // be careful not to emit spurious errors. Only matching
                    // patterns (match) can match nullary variants or
2345 2346 2347 2348
                    // unit-like structs. For binding patterns (let
                    // and the LHS of @-patterns), matching such a value is
                    // simply disallowed (since it's rarely what you want).
                    let const_ok = mode == RefutableMode && at_rhs.is_none();
2349

2350
                    let ident = path1.node;
2351
                    let renamed = ident.name;
2352

2353 2354
                    match self.resolve_bare_identifier_pattern(ident.unhygienic_name,
                                                               pattern.span) {
2355
                        FoundStructOrEnumVariant(def, lp) if const_ok => {
C
corentih 已提交
2356
                            debug!("(resolving pattern) resolving `{}` to struct or enum variant",
2357
                                   renamed);
2358

C
corentih 已提交
2359 2360 2361 2362 2363 2364 2365 2366 2367
                            self.enforce_default_binding_mode(pattern,
                                                              binding_mode,
                                                              "an enum variant");
                            self.record_def(pattern.id,
                                            PathResolution {
                                                base_def: def,
                                                last_private: lp,
                                                depth: 0,
                                            });
2368
                        }
A
Alex Crichton 已提交
2369
                        FoundStructOrEnumVariant(..) => {
2370
                            resolve_error(
2371
                                self,
2372
                                pattern.span,
2373
                                ResolutionError::DeclarationShadowsEnumVariantOrUnitLikeStruct(
2374 2375
                                    renamed)
                            );
2376
                            self.record_def(pattern.id, err_path_resolution());
2377
                        }
2378
                        FoundConst(def, lp, _) if const_ok => {
C
corentih 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387
                            debug!("(resolving pattern) resolving `{}` to constant", renamed);

                            self.enforce_default_binding_mode(pattern, binding_mode, "a constant");
                            self.record_def(pattern.id,
                                            PathResolution {
                                                base_def: def,
                                                last_private: lp,
                                                depth: 0,
                                            });
2388
                        }
2389
                        FoundConst(def, _, name) => {
2390
                            resolve_error(
2391 2392
                                self,
                                pattern.span,
M
Manish Goregaokar 已提交
2393 2394
                                ResolutionError::OnlyIrrefutablePatternsAllowedHere(def.def_id(),
                                                                                    name)
2395
                            );
2396
                            self.record_def(pattern.id, err_path_resolution());
2397
                        }
2398
                        BareIdentifierPatternUnresolved => {
C
corentih 已提交
2399
                            debug!("(resolving pattern) binding `{}`", renamed);
2400

2401
                            let def_id = self.ast_map.local_def_id(pattern.id);
2402
                            let def = Def::Local(def_id, pattern.id);
2403 2404 2405 2406 2407

                            // Record the definition so that later passes
                            // will be able to distinguish variants from
                            // locals in patterns.

C
corentih 已提交
2408 2409 2410 2411 2412 2413
                            self.record_def(pattern.id,
                                            PathResolution {
                                                base_def: def,
                                                last_private: LastMod(AllPublic),
                                                depth: 0,
                                            });
2414 2415 2416 2417 2418 2419

                            // Add the binding to the local ribs, if it
                            // doesn't already exist in the bindings list. (We
                            // must not add it if it's in the bindings list
                            // because that breaks the assumptions later
                            // passes make about or-patterns.)
2420 2421
                            if !bindings_list.contains_key(&renamed) {
                                let this = &mut *self;
2422 2423
                                let last_rib = this.value_ribs.last_mut().unwrap();
                                last_rib.bindings.insert(renamed, DlDef(def));
2424
                                bindings_list.insert(renamed, pat_id);
2425
                            } else if mode == ArgumentIrrefutableMode &&
C
corentih 已提交
2426
                               bindings_list.contains_key(&renamed) {
2427 2428
                                // Forbid duplicate bindings in the same
                                // parameter list.
2429
                                resolve_error(
2430 2431
                                    self,
                                    pattern.span,
2432
                                    ResolutionError::IdentifierBoundMoreThanOnceInParameterList(
2433
                                        &ident.name.as_str())
2434
                                );
C
corentih 已提交
2435
                            } else if bindings_list.get(&renamed) == Some(&pat_id) {
2436 2437
                                // Then this is a duplicate variable in the
                                // same disjunction, which is an error.
2438
                                resolve_error(
2439 2440
                                    self,
                                    pattern.span,
2441
                                    ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(
2442
                                        &ident.name.as_str())
2443
                                );
2444
                            }
2445 2446
                            // Else, not bound in the same pattern: do
                            // nothing.
2447 2448 2449 2450
                        }
                    }
                }

2451
                PatEnum(ref path, _) => {
2452
                    // This must be an enum variant, struct or const.
C
corentih 已提交
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
                    let resolution = match self.resolve_possibly_assoc_item(pat_id,
                                                                            None,
                                                                            path,
                                                                            ValueNS,
                                                                            false) {
                        // The below shouldn't happen because all
                        // qualified paths should be in PatQPath.
                        TypecheckRequired =>
                            self.session.span_bug(path.span,
                                                  "resolve_possibly_assoc_item claimed
                                     \
                                                   that a path in PatEnum requires typecheck
                                     \
                                                   to resolve, but qualified paths should be
                                     \
                                                   PatQPath"),
                        ResolveAttempt(resolution) => resolution,
                    };
2471
                    if let Some(path_res) = resolution {
2472
                        match path_res.base_def {
2473
                            Def::Struct(..) if path_res.depth == 0 => {
2474 2475
                                self.record_def(pattern.id, path_res);
                            }
2476
                            Def::Variant(..) | Def::Const(..) => {
2477 2478
                                self.record_def(pattern.id, path_res);
                            }
2479
                            Def::Static(..) => {
2480 2481
                                resolve_error(&self,
                                              path.span,
2482
                                              ResolutionError::StaticVariableReference);
2483
                                self.record_def(pattern.id, err_path_resolution());
2484
                            }
2485 2486 2487 2488
                            _ => {
                                // If anything ends up here entirely resolved,
                                // it's an error. If anything ends up here
                                // partially resolved, that's OK, because it may
2489
                                // be a `T::CONST` that typeck will resolve.
2490
                                if path_res.depth == 0 {
2491
                                    resolve_error(
2492
                                        self,
2493
                                        path.span,
2494
                                        ResolutionError::NotAnEnumVariantStructOrConst(
2495 2496 2497 2498 2499 2500
                                            &path.segments
                                                 .last()
                                                 .unwrap()
                                                 .identifier
                                                 .name
                                                 .as_str())
2501
                                    );
2502
                                    self.record_def(pattern.id, err_path_resolution());
2503
                                } else {
C
corentih 已提交
2504 2505 2506 2507 2508
                                    let const_name = path.segments
                                                         .last()
                                                         .unwrap()
                                                         .identifier
                                                         .name;
2509 2510
                                    let traits = self.get_traits_containing_item(const_name);
                                    self.trait_map.insert(pattern.id, traits);
2511 2512 2513 2514 2515
                                    self.record_def(pattern.id, path_res);
                                }
                            }
                        }
                    } else {
2516
                        resolve_error(
2517 2518
                            self,
                            path.span,
2519
                            ResolutionError::UnresolvedEnumVariantStructOrConst(
2520
                                &path.segments.last().unwrap().identifier.name.as_str())
2521
                        );
2522
                        self.record_def(pattern.id, err_path_resolution());
2523
                    }
2524
                    intravisit::walk_path(self, path);
2525 2526 2527 2528
                }

                PatQPath(ref qself, ref path) => {
                    // Associated constants only.
C
corentih 已提交
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
                    let resolution = match self.resolve_possibly_assoc_item(pat_id,
                                                                            Some(qself),
                                                                            path,
                                                                            ValueNS,
                                                                            false) {
                        TypecheckRequired => {
                            // All `<T>::CONST` should end up here, and will
                            // require use of the trait map to resolve
                            // during typechecking.
                            let const_name = path.segments
                                                 .last()
                                                 .unwrap()
                                                 .identifier
                                                 .name;
                            let traits = self.get_traits_containing_item(const_name);
                            self.trait_map.insert(pattern.id, traits);
2545
                            intravisit::walk_pat(self, pattern);
C
corentih 已提交
2546 2547 2548 2549
                            return true;
                        }
                        ResolveAttempt(resolution) => resolution,
                    };
2550 2551 2552 2553
                    if let Some(path_res) = resolution {
                        match path_res.base_def {
                            // All `<T as Trait>::CONST` should end up here, and
                            // have the trait already selected.
2554
                            Def::AssociatedConst(..) => {
2555 2556
                                self.record_def(pattern.id, path_res);
                            }
2557
                            _ => {
2558
                                resolve_error(
2559 2560
                                    self,
                                    path.span,
2561
                                    ResolutionError::NotAnAssociatedConst(
2562
                                        &path.segments.last().unwrap().identifier.name.as_str()
2563 2564
                                    )
                                );
2565
                                self.record_def(pattern.id, err_path_resolution());
2566
                            }
2567
                        }
2568
                    } else {
C
corentih 已提交
2569 2570 2571 2572 2573 2574 2575 2576
                        resolve_error(self,
                                      path.span,
                                      ResolutionError::UnresolvedAssociatedConst(&path.segments
                                                                                      .last()
                                                                                      .unwrap()
                                                                                      .identifier
                                                                                      .name
                                                                                      .as_str()));
2577
                        self.record_def(pattern.id, err_path_resolution());
2578
                    }
2579
                    intravisit::walk_pat(self, pattern);
2580 2581
                }

2582
                PatStruct(ref path, _, _) => {
2583
                    match self.resolve_path(pat_id, path, 0, TypeNS, false) {
2584
                        Some(definition) => {
2585 2586
                            self.record_def(pattern.id, definition);
                        }
2587
                        result => {
C
corentih 已提交
2588
                            debug!("(resolving pattern) didn't find struct def: {:?}", result);
2589 2590 2591
                            resolve_error(
                                self,
                                path.span,
2592
                                ResolutionError::DoesNotNameAStruct(
2593 2594
                                    &*path_names_to_string(path, 0))
                            );
2595
                            self.record_def(pattern.id, err_path_resolution());
2596 2597
                        }
                    }
2598
                    intravisit::walk_path(self, path);
2599 2600 2601
                }

                PatLit(_) | PatRange(..) => {
2602
                    intravisit::walk_pat(self, pattern);
2603 2604
                }

2605
                _ => {
2606 2607 2608
                    // Nothing to do.
                }
            }
2609
            true
2610
        });
2611 2612
    }

C
corentih 已提交
2613 2614 2615
    fn resolve_bare_identifier_pattern(&mut self,
                                       name: Name,
                                       span: Span)
E
Eduard Burtescu 已提交
2616
                                       -> BareIdentifierPatternResolution {
2617
        let module = self.current_module;
2618
        match self.resolve_item_in_lexical_scope(module, name, ValueNS, true) {
J
Jeffrey Seyfried 已提交
2619
            Success((binding, _)) => {
C
corentih 已提交
2620 2621
                debug!("(resolve bare identifier pattern) succeeded in finding {} at {:?}",
                       name,
J
Jeffrey Seyfried 已提交
2622 2623
                       &binding);
                match binding.def() {
B
Brian Anderson 已提交
2624
                    None => {
C
corentih 已提交
2625 2626
                        panic!("resolved name in the value namespace to a set of name bindings \
                                with no def?!");
2627
                    }
2628 2629 2630
                    // For the two success cases, this lookup can be
                    // considered as not having a private component because
                    // the lookup happened only within the current module.
2631
                    Some(def @ Def::Variant(..)) | Some(def @ Def::Struct(..)) => {
2632 2633
                        return FoundStructOrEnumVariant(def, LastMod(AllPublic));
                    }
2634
                    Some(def @ Def::Const(..)) | Some(def @ Def::AssociatedConst(..)) => {
2635
                        return FoundConst(def, LastMod(AllPublic), name);
2636
                    }
2637
                    Some(Def::Static(..)) => {
2638 2639
                        resolve_error(self, span, ResolutionError::StaticVariableReference);
                        return BareIdentifierPatternUnresolved;
2640
                    }
2641
                    _ => return BareIdentifierPatternUnresolved
2642 2643 2644
                }
            }

2645
            Indeterminate => return BareIdentifierPatternUnresolved,
2646 2647 2648
            Failed(err) => {
                match err {
                    Some((span, msg)) => {
2649
                        resolve_error(self, span, ResolutionError::FailedToResolve(&*msg));
2650
                    }
C
corentih 已提交
2651
                    None => (),
2652
                }
2653

C
corentih 已提交
2654
                debug!("(resolve bare identifier pattern) failed to find {}", name);
2655
                return BareIdentifierPatternUnresolved;
2656 2657 2658 2659
            }
        }
    }

2660 2661 2662
    /// Handles paths that may refer to associated items
    fn resolve_possibly_assoc_item(&mut self,
                                   id: NodeId,
2663
                                   maybe_qself: Option<&hir::QSelf>,
2664 2665 2666
                                   path: &Path,
                                   namespace: Namespace,
                                   check_ribs: bool)
C
corentih 已提交
2667
                                   -> AssocItemResolveResult {
2668 2669
        let max_assoc_types;

2670
        match maybe_qself {
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
            Some(qself) => {
                if qself.position == 0 {
                    return TypecheckRequired;
                }
                max_assoc_types = path.segments.len() - qself.position;
                // Make sure the trait is valid.
                let _ = self.resolve_trait_reference(id, path, max_assoc_types);
            }
            None => {
                max_assoc_types = path.segments.len();
            }
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
        }

        let mut resolution = self.with_no_errors(|this| {
            this.resolve_path(id, path, 0, namespace, check_ribs)
        });
        for depth in 1..max_assoc_types {
            if resolution.is_some() {
                break;
            }
            self.with_no_errors(|this| {
C
corentih 已提交
2692
                resolution = this.resolve_path(id, path, depth, TypeNS, true);
2693 2694
            });
        }
2695
        if let Some(Def::Mod(_)) = resolution.map(|r| r.base_def) {
2696 2697 2698 2699 2700 2701
            // A module is not a valid type or value.
            resolution = None;
        }
        ResolveAttempt(resolution)
    }

2702 2703
    /// If `check_ribs` is true, checks the local definitions first; i.e.
    /// doesn't skip straight to the containing module.
2704 2705
    /// Skips `path_depth` trailing segments, which is also reflected in the
    /// returned value. See `middle::def::PathResolution` for more info.
G
Garming Sam 已提交
2706 2707 2708 2709 2710
    pub fn resolve_path(&mut self,
                        id: NodeId,
                        path: &Path,
                        path_depth: usize,
                        namespace: Namespace,
C
corentih 已提交
2711 2712
                        check_ribs: bool)
                        -> Option<PathResolution> {
2713
        let span = path.span;
C
corentih 已提交
2714
        let segments = &path.segments[..path.segments.len() - path_depth];
2715

N
Nick Cameron 已提交
2716
        let mk_res = |(def, lp)| PathResolution::new(def, lp, path_depth);
2717

2718
        if path.global {
2719
            let def = self.resolve_crate_relative_path(span, segments, namespace);
2720
            return def.map(mk_res);
2721 2722
        }

2723
        // Try to find a path to an item in a module.
2724
        let last_ident = segments.last().unwrap().identifier;
N
Nick Cameron 已提交
2725
        if segments.len() <= 1 {
2726
            let unqualified_def = self.resolve_identifier(last_ident, namespace, check_ribs, true);
C
corentih 已提交
2727 2728 2729 2730
            return unqualified_def.and_then(|def| self.adjust_local_def(def, span))
                                  .map(|def| {
                                      PathResolution::new(def, LastMod(AllPublic), path_depth)
                                  });
N
Nick Cameron 已提交
2731
        }
2732

2733
        let unqualified_def = self.resolve_identifier(last_ident, namespace, check_ribs, false);
N
Nick Cameron 已提交
2734 2735
        let def = self.resolve_module_relative_path(span, segments, namespace);
        match (def, unqualified_def) {
2736
            (Some((ref d, _)), Some(ref ud)) if *d == ud.def => {
N
Nick Cameron 已提交
2737 2738
                self.session
                    .add_lint(lint::builtin::UNUSED_QUALIFICATIONS,
C
corentih 已提交
2739 2740
                              id,
                              span,
N
Nick Cameron 已提交
2741 2742 2743
                              "unnecessary qualification".to_string());
            }
            _ => {}
2744
        }
N
Nick Cameron 已提交
2745 2746

        def.map(mk_res)
2747 2748
    }

2749
    // Resolve a single identifier
F
Felix S. Klock II 已提交
2750
    fn resolve_identifier(&mut self,
2751
                          identifier: hir::Ident,
2752
                          namespace: Namespace,
2753 2754
                          check_ribs: bool,
                          record_used: bool)
2755
                          -> Option<LocalDef> {
2756
        if identifier.name == special_idents::invalid.name {
2757
            return Some(LocalDef::from_def(Def::Err));
2758 2759
        }

2760 2761 2762 2763
        // First, check to see whether the name is a primitive type.
        if namespace == TypeNS {
            if let Some(&prim_ty) = self.primitive_type_table
                                        .primitive_types
2764
                                        .get(&identifier.unhygienic_name) {
2765
                return Some(LocalDef::from_def(Def::PrimTy(prim_ty)));
2766 2767 2768
            }
        }

2769
        if check_ribs {
C
corentih 已提交
2770
            if let Some(def) = self.resolve_identifier_in_local_ribs(identifier, namespace) {
2771
                return Some(def);
2772 2773 2774
            }
        }

2775 2776
        // Check the items.
        let module = self.current_module;
2777
        let name = identifier.unhygienic_name;
2778
        match self.resolve_item_in_lexical_scope(module, name, namespace, record_used) {
J
Jeffrey Seyfried 已提交
2779
            Success((binding, _)) => binding.def().map(LocalDef::from_def),
2780 2781 2782 2783 2784 2785
            Failed(Some((span, msg))) => {
                resolve_error(self, span, ResolutionError::FailedToResolve(&*msg));
                None
            }
            _ => None,
        }
2786 2787 2788
    }

    // Resolve a local definition, potentially adjusting for closures.
2789
    fn adjust_local_def(&mut self, local_def: LocalDef, span: Span) -> Option<Def> {
2790
        let ribs = match local_def.ribs {
C
corentih 已提交
2791 2792 2793
            Some((TypeNS, i)) => &self.type_ribs[i + 1..],
            Some((ValueNS, i)) => &self.value_ribs[i + 1..],
            _ => &[] as &[_],
2794 2795 2796
        };
        let mut def = local_def.def;
        match def {
2797
            Def::Upvar(..) => {
C
corentih 已提交
2798
                self.session.span_bug(span, &format!("unexpected {:?} in bindings", def))
2799
            }
2800
            Def::Local(_, node_id) => {
2801 2802
                for rib in ribs {
                    match rib.kind {
2803
                        NormalRibKind | AnonymousModuleRibKind(..) => {
2804 2805 2806 2807 2808 2809
                            // Nothing to do. Continue.
                        }
                        ClosureRibKind(function_id) => {
                            let prev_def = def;
                            let node_def_id = self.ast_map.local_def_id(node_id);

C
corentih 已提交
2810 2811 2812
                            let seen = self.freevars_seen
                                           .entry(function_id)
                                           .or_insert_with(|| NodeMap());
2813
                            if let Some(&index) = seen.get(&node_id) {
2814
                                def = Def::Upvar(node_def_id, node_id, index, function_id);
2815 2816
                                continue;
                            }
C
corentih 已提交
2817 2818 2819
                            let vec = self.freevars
                                          .entry(function_id)
                                          .or_insert_with(|| vec![]);
2820
                            let depth = vec.len();
C
corentih 已提交
2821 2822 2823 2824
                            vec.push(Freevar {
                                def: prev_def,
                                span: span,
                            });
2825

2826
                            def = Def::Upvar(node_def_id, node_id, depth, function_id);
2827 2828 2829 2830 2831 2832
                            seen.insert(node_id, depth);
                        }
                        ItemRibKind | MethodRibKind => {
                            // This was an attempt to access an upvar inside a
                            // named function item. This is not allowed, so we
                            // report an error.
C
corentih 已提交
2833 2834 2835
                            resolve_error(self,
                                          span,
                                          ResolutionError::CannotCaptureDynamicEnvironmentInFnItem);
2836 2837 2838 2839
                            return None;
                        }
                        ConstantItemRibKind => {
                            // Still doesn't deal with upvars
C
corentih 已提交
2840 2841 2842
                            resolve_error(self,
                                          span,
                                          ResolutionError::AttemptToUseNonConstantValueInConstant);
2843 2844 2845 2846 2847
                            return None;
                        }
                    }
                }
            }
2848
            Def::TyParam(..) | Def::SelfTy(..) => {
2849 2850
                for rib in ribs {
                    match rib.kind {
2851 2852
                        NormalRibKind | MethodRibKind | ClosureRibKind(..) |
                        AnonymousModuleRibKind(..) => {
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
                            // Nothing to do. Continue.
                        }
                        ItemRibKind => {
                            // This was an attempt to use a type parameter outside
                            // its scope.

                            resolve_error(self,
                                          span,
                                          ResolutionError::TypeParametersFromOuterFunction);
                            return None;
                        }
                        ConstantItemRibKind => {
                            // see #9186
                            resolve_error(self, span, ResolutionError::OuterTypeParameterContext);
                            return None;
                        }
                    }
                }
            }
            _ => {}
        }
        return Some(def);
2875 2876
    }

2877
    // resolve a "module-relative" path, e.g. a::b::c
F
Felix S. Klock II 已提交
2878
    fn resolve_module_relative_path(&mut self,
2879
                                    span: Span,
2880
                                    segments: &[hir::PathSegment],
2881 2882
                                    namespace: Namespace)
                                    -> Option<(Def, LastPrivate)> {
C
corentih 已提交
2883 2884 2885 2886 2887 2888
        let module_path = segments.split_last()
                                  .unwrap()
                                  .1
                                  .iter()
                                  .map(|ps| ps.identifier.name)
                                  .collect::<Vec<_>>();
2889

2890
        let containing_module;
2891
        let last_private;
2892
        let current_module = self.current_module;
J
Jeffrey Seyfried 已提交
2893
        match self.resolve_module_path(current_module, &module_path, UseLexicalScope, span) {
2894 2895 2896 2897
            Failed(err) => {
                let (span, msg) = match err {
                    Some((span, msg)) => (span, msg),
                    None => {
2898
                        let msg = format!("Use of undeclared type or module `{}`",
2899
                                          names_to_string(&module_path));
2900
                        (span, msg)
2901 2902
                    }
                };
2903

2904
                resolve_error(self, span, ResolutionError::FailedToResolve(&*msg));
2905
                return None;
2906
            }
2907
            Indeterminate => return None,
2908
            Success((resulting_module, resulting_last_private)) => {
2909
                containing_module = resulting_module;
2910
                last_private = resulting_last_private;
2911 2912 2913
            }
        }

2914
        let name = segments.last().unwrap().identifier.name;
2915 2916
        let result = self.resolve_name_in_module(containing_module, name, namespace, false, true);
        let def = match result {
J
Jeffrey Seyfried 已提交
2917
            Success((binding, _)) => {
2918
                let (def, lp) = binding.def_and_lp();
2919
                (def, last_private.or(lp))
2920
            }
2921
            _ => return None,
2922 2923
        };
        return Some(def);
2924 2925
    }

2926 2927
    /// Invariant: This must be called only during main resolution, not during
    /// import resolution.
F
Felix S. Klock II 已提交
2928
    fn resolve_crate_relative_path(&mut self,
2929
                                   span: Span,
2930
                                   segments: &[hir::PathSegment],
2931
                                   namespace: Namespace)
C
corentih 已提交
2932 2933 2934 2935 2936 2937 2938
                                   -> Option<(Def, LastPrivate)> {
        let module_path = segments.split_last()
                                  .unwrap()
                                  .1
                                  .iter()
                                  .map(|ps| ps.identifier.name)
                                  .collect::<Vec<_>>();
2939

2940
        let root_module = self.graph_root;
2941

2942
        let containing_module;
2943
        let last_private;
2944
        match self.resolve_module_path_from_root(root_module,
2945
                                                 &module_path,
2946
                                                 0,
2947
                                                 span,
2948
                                                 LastMod(AllPublic)) {
2949 2950 2951 2952 2953
            Failed(err) => {
                let (span, msg) = match err {
                    Some((span, msg)) => (span, msg),
                    None => {
                        let msg = format!("Use of undeclared module `::{}`",
2954
                                          names_to_string(&module_path));
2955
                        (span, msg)
2956 2957 2958
                    }
                };

2959
                resolve_error(self, span, ResolutionError::FailedToResolve(&*msg));
B
Brian Anderson 已提交
2960
                return None;
2961 2962
            }

2963
            Indeterminate => return None,
2964

2965
            Success((resulting_module, resulting_last_private)) => {
2966
                containing_module = resulting_module;
2967
                last_private = resulting_last_private;
2968 2969 2970
            }
        }

2971
        let name = segments.last().unwrap().identifier.name;
2972
        match self.resolve_name_in_module(containing_module, name, namespace, false, true) {
J
Jeffrey Seyfried 已提交
2973
            Success((binding, _)) => {
2974 2975
                let (def, lp) = binding.def_and_lp();
                Some((def, last_private.or(lp)))
2976
            }
2977
            _ => None,
2978 2979 2980
        }
    }

F
Felix S. Klock II 已提交
2981
    fn resolve_identifier_in_local_ribs(&mut self,
2982
                                        ident: hir::Ident,
2983 2984
                                        namespace: Namespace)
                                        -> Option<LocalDef> {
2985
        // Check the local set of ribs.
2986
        let name = match namespace { ValueNS => ident.name, TypeNS => ident.unhygienic_name };
2987

2988 2989
        for i in (0 .. self.get_ribs(namespace).len()).rev() {
            if let Some(def_like) = self.get_ribs(namespace)[i].bindings.get(&name).cloned() {
2990 2991 2992
                match def_like {
                    DlDef(def) => {
                        debug!("(resolving path in local ribs) resolved `{}` to {:?} at {}",
C
corentih 已提交
2993 2994 2995
                               name,
                               def,
                               i);
2996 2997
                        return Some(LocalDef {
                            ribs: Some((namespace, i)),
C
corentih 已提交
2998
                            def: def,
2999 3000 3001 3002
                        });
                    }
                    def_like => {
                        debug!("(resolving path in local ribs) resolved `{}` to pseudo-def {:?}",
C
corentih 已提交
3003 3004
                               name,
                               def_like);
3005 3006 3007
                        return None;
                    }
                }
3008
            }
3009 3010

            if let AnonymousModuleRibKind(module) = self.get_ribs(namespace)[i].kind {
J
Jeffrey Seyfried 已提交
3011 3012 3013 3014 3015 3016
                if let Success((binding, _)) = self.resolve_name_in_module(module,
                                                                           ident.unhygienic_name,
                                                                           namespace,
                                                                           true,
                                                                           true) {
                    if let Some(def) = binding.def() {
3017 3018 3019 3020
                        return Some(LocalDef::from_def(def));
                    }
                }
            }
3021
        }
3022 3023

        None
3024 3025
    }

C
corentih 已提交
3026 3027
    fn with_no_errors<T, F>(&mut self, f: F) -> T
        where F: FnOnce(&mut Resolver) -> T
J
Jorge Aparicio 已提交
3028
    {
3029
        self.emit_errors = false;
A
Alex Crichton 已提交
3030
        let rs = f(self);
3031 3032 3033 3034
        self.emit_errors = true;
        rs
    }

3035
    fn find_fallback_in_self_type(&mut self, name: Name) -> FallbackSuggestion {
C
corentih 已提交
3036 3037 3038
        fn extract_path_and_node_id(t: &Ty,
                                    allow: FallbackChecks)
                                    -> Option<(Path, NodeId, FallbackChecks)> {
3039
            match t.node {
3040
                TyPath(None, ref path) => Some((path.clone(), t.id, allow)),
3041 3042
                TyPtr(ref mut_ty) => extract_path_and_node_id(&*mut_ty.ty, OnlyTraitAndStatics),
                TyRptr(_, ref mut_ty) => extract_path_and_node_id(&*mut_ty.ty, allow),
3043 3044 3045 3046 3047 3048 3049
                // This doesn't handle the remaining `Ty` variants as they are not
                // that commonly the self_type, it might be interesting to provide
                // support for those in future.
                _ => None,
            }
        }

3050 3051 3052 3053 3054
        fn get_module<'a, 'tcx>(this: &mut Resolver<'a, 'tcx>,
                                span: Span,
                                name_path: &[ast::Name])
                                -> Option<Module<'a>> {
            let root = this.current_module;
3055
            let last_name = name_path.last().unwrap();
3056

3057
            if name_path.len() == 1 {
3058
                match this.primitive_type_table.primitive_types.get(last_name) {
3059
                    Some(_) => None,
3060 3061 3062
                    None => this.current_module.get_child(*last_name, TypeNS)
                                               .as_ref()
                                               .and_then(NameBinding::module)
3063 3064
                }
            } else {
J
Jeffrey Seyfried 已提交
3065
                match this.resolve_module_path(root, &name_path, UseLexicalScope, span) {
3066
                    Success((module, _)) => Some(module),
C
corentih 已提交
3067
                    _ => None,
3068 3069 3070 3071
                }
            }
        }

3072
        fn is_static_method(this: &Resolver, did: DefId) -> bool {
3073 3074
            if let Some(node_id) = this.ast_map.as_local_node_id(did) {
                let sig = match this.ast_map.get(node_id) {
3075 3076
                    hir_map::NodeTraitItem(trait_item) => match trait_item.node {
                        hir::MethodTraitItem(ref sig, _) => sig,
C
corentih 已提交
3077
                        _ => return false,
3078
                    },
3079
                    hir_map::NodeImplItem(impl_item) => match impl_item.node {
3080
                        hir::ImplItemKind::Method(ref sig, _) => sig,
C
corentih 已提交
3081
                        _ => return false,
3082
                    },
C
corentih 已提交
3083
                    _ => return false,
3084
                };
3085
                sig.explicit_self.node == hir::SelfStatic
3086
            } else {
3087
                this.session.cstore.is_static_method(did)
3088 3089 3090
            }
        }

3091 3092 3093 3094
        let (path, node_id, allowed) = match self.current_self_type {
            Some(ref ty) => match extract_path_and_node_id(ty, Everything) {
                Some(x) => x,
                None => return NoSuggestion,
3095 3096 3097 3098
            },
            None => return NoSuggestion,
        };

3099 3100
        if allowed == Everything {
            // Look for a field with the same name in the current self_type.
3101
            match self.def_map.borrow().get(&node_id).map(|d| d.full_def()) {
3102 3103 3104 3105
                Some(Def::Enum(did)) |
                Some(Def::TyAlias(did)) |
                Some(Def::Struct(did)) |
                Some(Def::Variant(_, did)) => match self.structs.get(&did) {
3106 3107 3108 3109 3110
                    None => {}
                    Some(fields) => {
                        if fields.iter().any(|&field_name| name == field_name) {
                            return Field;
                        }
3111
                    }
3112 3113 3114
                },
                _ => {} // Self type didn't resolve properly
            }
3115 3116
        }

3117
        let name_path = path.segments.iter().map(|seg| seg.identifier.name).collect::<Vec<_>>();
3118 3119

        // Look for a method in the current self type's impl module.
3120
        if let Some(module) = get_module(self, path.span, &name_path) {
3121 3122
            if let Some(binding) = module.get_child(name, ValueNS) {
                if let Some(Def::Method(did)) = binding.def() {
3123
                    if is_static_method(self, did) {
C
corentih 已提交
3124
                        return StaticMethod(path_names_to_string(&path, 0));
3125 3126 3127 3128 3129
                    }
                    if self.current_trait_ref.is_some() {
                        return TraitItem;
                    } else if allowed == Everything {
                        return Method;
3130 3131
                    }
                }
3132
            }
3133 3134 3135
        }

        // Look for a method in the current trait.
3136 3137 3138
        if let Some((trait_did, ref trait_ref)) = self.current_trait_ref {
            if let Some(&did) = self.trait_item_map.get(&(name, trait_did)) {
                if is_static_method(self, did) {
3139
                    return TraitMethod(path_names_to_string(&trait_ref.path, 0));
3140 3141
                } else {
                    return TraitItem;
3142 3143 3144 3145 3146 3147 3148
                }
            }
        }

        NoSuggestion
    }

3149
    fn find_best_match(&mut self, name: &str) -> SuggestionType {
3150
        if let Some(macro_name) = self.session.available_macros
3151
                                  .borrow().iter().find(|n| n.as_str() == name) {
3152 3153 3154
            return SuggestionType::Macro(format!("{}!", macro_name));
        }

3155 3156 3157 3158
        let names = self.value_ribs
                    .iter()
                    .rev()
                    .flat_map(|rib| rib.bindings.keys());
3159

3160 3161 3162
        if let Some(found) = find_best_match_for_name(names, name, None) {
            if name != &*found {
                return SuggestionType::Function(found);
3163
            }
3164
        } SuggestionType::NotFound
3165 3166
    }

E
Eduard Burtescu 已提交
3167
    fn resolve_expr(&mut self, expr: &Expr) {
P
Patrick Walton 已提交
3168 3169
        // First, record candidate traits for this expression if it could
        // result in the invocation of a method call.
3170 3171 3172

        self.record_candidate_traits_for_expr_if_necessary(expr);

3173
        // Next, resolve the node.
3174
        match expr.node {
3175
            ExprPath(ref maybe_qself, ref path) => {
C
corentih 已提交
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
                let resolution = match self.resolve_possibly_assoc_item(expr.id,
                                                                        maybe_qself.as_ref(),
                                                                        path,
                                                                        ValueNS,
                                                                        true) {
                    // `<T>::a::b::c` is resolved by typeck alone.
                    TypecheckRequired => {
                        let method_name = path.segments.last().unwrap().identifier.name;
                        let traits = self.get_traits_containing_item(method_name);
                        self.trait_map.insert(expr.id, traits);
3186
                        intravisit::walk_expr(self, expr);
C
corentih 已提交
3187 3188 3189 3190
                        return;
                    }
                    ResolveAttempt(resolution) => resolution,
                };
3191

3192 3193
                // This is a local path in the value namespace. Walk through
                // scopes looking for it.
3194
                if let Some(path_res) = resolution {
3195
                    // Check if struct variant
3196
                    let is_struct_variant = if let Def::Variant(_, variant_id) = path_res.base_def {
3197 3198 3199 3200 3201 3202
                        self.structs.contains_key(&variant_id)
                    } else {
                        false
                    };
                    if is_struct_variant {
                        let _ = self.structs.contains_key(&path_res.base_def.def_id());
3203
                        let path_name = path_names_to_string(path, 0);
3204

N
Nick Cameron 已提交
3205 3206 3207
                        let mut err = resolve_struct_error(self,
                                        expr.span,
                                        ResolutionError::StructVariantUsedAsFunction(&*path_name));
3208

C
corentih 已提交
3209
                        let msg = format!("did you mean to write: `{} {{ /* fields */ }}`?",
3210 3211
                                          path_name);
                        if self.emit_errors {
N
Nick Cameron 已提交
3212
                            err.fileline_help(expr.span, &msg);
3213
                        } else {
N
Nick Cameron 已提交
3214
                            err.span_help(expr.span, &msg);
3215
                        }
N
Nick Cameron 已提交
3216
                        err.emit();
3217
                        self.record_def(expr.id, err_path_resolution());
3218
                    } else {
3219
                        // Write the result into the def map.
3220
                        debug!("(resolving expr) resolved `{}`",
3221
                               path_names_to_string(path, 0));
3222

3223 3224
                        // Partial resolutions will need the set of traits in scope,
                        // so they can be completed during typeck.
3225
                        if path_res.depth != 0 {
3226
                            let method_name = path.segments.last().unwrap().identifier.name;
3227
                            let traits = self.get_traits_containing_item(method_name);
3228 3229 3230
                            self.trait_map.insert(expr.id, traits);
                        }

3231
                        self.record_def(expr.id, path_res);
3232
                    }
3233 3234 3235 3236 3237
                } else {
                    // Be helpful if the name refers to a struct
                    // (The pattern matching def_tys where the id is in self.structs
                    // matches on regular structs while excluding tuple- and enum-like
                    // structs, which wouldn't result in this error.)
3238
                    let path_name = path_names_to_string(path, 0);
3239 3240 3241
                    let type_res = self.with_no_errors(|this| {
                        this.resolve_path(expr.id, path, 0, TypeNS, false)
                    });
3242 3243

                    self.record_def(expr.id, err_path_resolution());
3244
                    match type_res.map(|r| r.base_def) {
3245
                        Some(Def::Struct(..)) => {
N
Nick Cameron 已提交
3246 3247 3248
                            let mut err = resolve_struct_error(self,
                                expr.span,
                                ResolutionError::StructVariantUsedAsFunction(&*path_name));
3249

C
corentih 已提交
3250 3251 3252
                            let msg = format!("did you mean to write: `{} {{ /* fields */ }}`?",
                                              path_name);
                            if self.emit_errors {
N
Nick Cameron 已提交
3253
                                err.fileline_help(expr.span, &msg);
C
corentih 已提交
3254
                            } else {
N
Nick Cameron 已提交
3255
                                err.span_help(expr.span, &msg);
3256
                            }
N
Nick Cameron 已提交
3257
                            err.emit();
C
corentih 已提交
3258
                        }
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271
                        _ => {
                            // Keep reporting some errors even if they're ignored above.
                            self.resolve_path(expr.id, path, 0, ValueNS, true);

                            let mut method_scope = false;
                            self.value_ribs.iter().rev().all(|rib| {
                                method_scope = match rib.kind {
                                    MethodRibKind => true,
                                    ItemRibKind | ConstantItemRibKind => false,
                                    _ => return true, // Keep advancing
                                };
                                false // Stop advancing
                            });
3272

3273
                            if method_scope && special_names::self_.as_str() == &path_name[..] {
C
corentih 已提交
3274 3275 3276
                                resolve_error(self,
                                              expr.span,
                                              ResolutionError::SelfNotAvailableInStaticMethod);
3277 3278 3279 3280 3281 3282
                            } else {
                                let last_name = path.segments.last().unwrap().identifier.name;
                                let mut msg = match self.find_fallback_in_self_type(last_name) {
                                    NoSuggestion => {
                                        // limit search to 5 to reduce the number
                                        // of stupid suggestions
3283
                                        match self.find_best_match(&path_name) {
3284 3285 3286 3287 3288 3289
                                            SuggestionType::Macro(s) => {
                                                format!("the macro `{}`", s)
                                            }
                                            SuggestionType::Function(s) => format!("`{}`", s),
                                            SuggestionType::NotFound => "".to_string(),
                                        }
3290 3291 3292
                                    }
                                    Field => format!("`self.{}`", path_name),
                                    Method |
C
corentih 已提交
3293
                                    TraitItem => format!("to call `self.{}`", path_name),
3294 3295
                                    TraitMethod(path_str) |
                                    StaticMethod(path_str) =>
C
corentih 已提交
3296
                                        format!("to call `{}::{}`", path_str, path_name),
3297 3298
                                };

3299
                                let mut context =  UnresolvedNameContext::Other;
3300
                                if !msg.is_empty() {
3301 3302 3303 3304 3305 3306 3307
                                    msg = format!(". Did you mean {}?", msg);
                                } else {
                                    // we check if this a module and if so, we display a help
                                    // message
                                    let name_path = path.segments.iter()
                                                        .map(|seg| seg.identifier.name)
                                                        .collect::<Vec<_>>();
3308
                                    let current_module = self.current_module;
3309 3310

                                    match self.resolve_module_path(current_module,
J
Jeffrey Seyfried 已提交
3311 3312 3313
                                                                   &name_path[..],
                                                                   UseLexicalScope,
                                                                   expr.span) {
3314 3315 3316 3317 3318
                                        Success(_) => {
                                            context = UnresolvedNameContext::PathIsMod(expr.id);
                                        },
                                        _ => {},
                                    };
3319
                                }
3320

3321 3322
                                resolve_error(self,
                                              expr.span,
3323 3324
                                              ResolutionError::UnresolvedName(
                                                  &*path_name, &*msg, context));
3325
                            }
V
Vincent Belliard 已提交
3326
                        }
3327 3328 3329
                    }
                }

3330
                intravisit::walk_expr(self, expr);
3331 3332
            }

3333
            ExprStruct(ref path, _, _) => {
3334 3335 3336
                // Resolve the path to the structure it goes to. We don't
                // check to ensure that the path is actually a structure; that
                // is checked later during typeck.
3337
                match self.resolve_path(expr.id, path, 0, TypeNS, false) {
3338
                    Some(definition) => self.record_def(expr.id, definition),
3339 3340
                    None => {
                        debug!("(resolving expression) didn't find struct def",);
3341

3342 3343
                        resolve_error(self,
                                      path.span,
3344
                                      ResolutionError::DoesNotNameAStruct(
3345 3346
                                                                &*path_names_to_string(path, 0))
                                     );
3347
                        self.record_def(expr.id, err_path_resolution());
3348 3349 3350
                    }
                }

3351
                intravisit::walk_expr(self, expr);
3352 3353
            }

P
Pythoner6 已提交
3354
            ExprLoop(_, Some(label)) | ExprWhile(_, _, Some(label)) => {
3355
                self.with_label_rib(|this| {
3356
                    let def_like = DlDef(Def::Label(expr.id));
3357

3358
                    {
3359
                        let rib = this.label_ribs.last_mut().unwrap();
3360
                        rib.bindings.insert(label.name, def_like);
3361
                    }
3362

3363
                    intravisit::walk_expr(this, expr);
3364
                })
3365 3366
            }

3367
            ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
3368
                match self.search_label(label.node.name) {
3369
                    None => {
3370
                        self.record_def(expr.id, err_path_resolution());
3371
                        resolve_error(self,
3372 3373
                                      label.span,
                                      ResolutionError::UndeclaredLabel(&label.node.name.as_str()))
3374
                    }
3375
                    Some(DlDef(def @ Def::Label(_))) => {
3376
                        // Since this def is a label, it is never read.
C
corentih 已提交
3377 3378 3379 3380 3381 3382
                        self.record_def(expr.id,
                                        PathResolution {
                                            base_def: def,
                                            last_private: LastMod(AllPublic),
                                            depth: 0,
                                        })
3383 3384
                    }
                    Some(_) => {
C
corentih 已提交
3385
                        self.session.span_bug(expr.span, "label wasn't mapped to a label def!")
3386 3387 3388 3389
                    }
                }
            }

B
Brian Anderson 已提交
3390
            _ => {
3391
                intravisit::walk_expr(self, expr);
3392 3393 3394 3395
            }
        }
    }

E
Eduard Burtescu 已提交
3396
    fn record_candidate_traits_for_expr_if_necessary(&mut self, expr: &Expr) {
3397
        match expr.node {
3398
            ExprField(_, name) => {
3399 3400 3401 3402
                // FIXME(#6890): Even though you can't treat a method like a
                // field, we need to add any trait methods we find that match
                // the field name so that we can do some nice error reporting
                // later on in typeck.
3403
                let traits = self.get_traits_containing_item(name.node);
3404
                self.trait_map.insert(expr.id, traits);
3405
            }
3406
            ExprMethodCall(name, _, _) => {
C
corentih 已提交
3407
                debug!("(recording candidate traits for expr) recording traits for {}",
3408
                       expr.id);
3409
                let traits = self.get_traits_containing_item(name.node);
3410
                self.trait_map.insert(expr.id, traits);
3411
            }
3412
            _ => {
3413 3414 3415 3416 3417
                // Nothing to do.
            }
        }
    }

3418
    fn get_traits_containing_item(&mut self, name: Name) -> Vec<DefId> {
C
corentih 已提交
3419
        debug!("(getting traits containing item) looking for '{}'", name);
E
Eduard Burtescu 已提交
3420

C
corentih 已提交
3421
        fn add_trait_info(found_traits: &mut Vec<DefId>, trait_def_id: DefId, name: Name) {
3422
            debug!("(adding trait info) found trait {:?} for method '{}'",
C
corentih 已提交
3423 3424
                   trait_def_id,
                   name);
E
Eduard Burtescu 已提交
3425 3426
            found_traits.push(trait_def_id);
        }
3427

3428
        let mut found_traits = Vec::new();
3429
        let mut search_module = self.current_module;
E
Eduard Burtescu 已提交
3430 3431
        loop {
            // Look for the current trait.
3432 3433
            match self.current_trait_ref {
                Some((trait_def_id, _)) => {
3434
                    if self.trait_item_map.contains_key(&(name, trait_def_id)) {
3435
                        add_trait_info(&mut found_traits, trait_def_id, name);
3436 3437
                    }
                }
3438
                None => {} // Nothing to do.
E
Eduard Burtescu 已提交
3439
            }
3440

E
Eduard Burtescu 已提交
3441
            // Look for trait children.
3442
            build_reduced_graph::populate_module_if_necessary(self, &search_module);
3443

3444 3445 3446 3447 3448 3449 3450 3451
            for (&(_, ns), name_binding) in search_module.children.borrow().iter() {
                if ns != TypeNS { continue }
                let trait_def_id = match name_binding.def() {
                    Some(Def::Trait(trait_def_id)) => trait_def_id,
                    Some(..) | None => continue,
                };
                if self.trait_item_map.contains_key(&(name, trait_def_id)) {
                    add_trait_info(&mut found_traits, trait_def_id, name);
3452
                }
E
Eduard Burtescu 已提交
3453
            }
3454

E
Eduard Burtescu 已提交
3455
            // Look for imports.
3456
            for (&(_, ns), import) in search_module.import_resolutions.borrow().iter() {
3457
                if ns != TypeNS { continue }
J
Jeffrey Seyfried 已提交
3458 3459
                let binding = match import.binding {
                    Some(ref binding) => binding,
3460
                    None => continue,
E
Eduard Burtescu 已提交
3461
                };
J
Jeffrey Seyfried 已提交
3462
                let did = match binding.def() {
3463
                    Some(Def::Trait(trait_def_id)) => trait_def_id,
E
Eduard Burtescu 已提交
3464 3465
                    Some(..) | None => continue,
                };
3466
                if self.trait_item_map.contains_key(&(name, did)) {
E
Eduard Burtescu 已提交
3467
                    add_trait_info(&mut found_traits, did, name);
3468
                    let trait_name = self.get_trait_name(did);
3469
                    self.record_import_use(trait_name, TypeNS, &import);
3470
                }
E
Eduard Burtescu 已提交
3471
            }
3472

3473
            match search_module.parent_link {
E
Eduard Burtescu 已提交
3474 3475
                NoParentLink | ModuleParentLink(..) => break,
                BlockParentLink(parent_module, _) => {
3476
                    search_module = parent_module;
3477
                }
E
Eduard Burtescu 已提交
3478
            }
3479 3480
        }

E
Eduard Burtescu 已提交
3481
        found_traits
3482 3483
    }

3484 3485
    fn record_def(&mut self, node_id: NodeId, resolution: PathResolution) {
        debug!("(recording def) recording {:?} for {}", resolution, node_id);
C
corentih 已提交
3486 3487 3488 3489
        assert!(match resolution.last_private {
                    LastImport{..} => false,
                    _ => true,
                },
3490
                "Import should only be used for `use` directives");
3491

3492 3493
        if let Some(prev_res) = self.def_map.borrow_mut().insert(node_id, resolution) {
            let span = self.ast_map.opt_span(node_id).unwrap_or(codemap::DUMMY_SP);
C
corentih 已提交
3494 3495 3496 3497
            self.session.span_bug(span,
                                  &format!("path resolved multiple times ({:?} before, {:?} now)",
                                           prev_res,
                                           resolution));
3498
        }
3499 3500
    }

F
Felix S. Klock II 已提交
3501
    fn enforce_default_binding_mode(&mut self,
C
corentih 已提交
3502 3503 3504
                                    pat: &Pat,
                                    pat_binding_mode: BindingMode,
                                    descr: &str) {
3505
        match pat_binding_mode {
3506
            BindByValue(_) => {}
A
Alex Crichton 已提交
3507
            BindByRef(..) => {
3508 3509
                resolve_error(self,
                              pat.span,
3510
                              ResolutionError::CannotUseRefBindingModeWith(descr));
3511 3512 3513
            }
        }
    }
3514 3515
}

3516 3517 3518 3519 3520 3521 3522 3523 3524 3525

fn names_to_string(names: &[Name]) -> String {
    let mut first = true;
    let mut result = String::new();
    for name in names {
        if first {
            first = false
        } else {
            result.push_str("::")
        }
3526
        result.push_str(&name.as_str());
C
corentih 已提交
3527
    }
3528 3529 3530 3531
    result
}

fn path_names_to_string(path: &Path, depth: usize) -> String {
C
corentih 已提交
3532
    let names: Vec<ast::Name> = path.segments[..path.segments.len() - depth]
3533 3534 3535 3536 3537 3538 3539
                                    .iter()
                                    .map(|seg| seg.identifier.name)
                                    .collect();
    names_to_string(&names[..])
}

/// A somewhat inefficient routine to obtain the name of a module.
3540
fn module_to_string<'a>(module: Module<'a>) -> String {
3541 3542
    let mut names = Vec::new();

3543
    fn collect_mod<'a>(names: &mut Vec<ast::Name>, module: Module<'a>) {
3544 3545 3546 3547
        match module.parent_link {
            NoParentLink => {}
            ModuleParentLink(ref module, name) => {
                names.push(name);
3548
                collect_mod(names, module);
3549 3550 3551 3552
            }
            BlockParentLink(ref module, _) => {
                // danger, shouldn't be ident?
                names.push(special_idents::opaque.name);
3553
                collect_mod(names, module);
3554 3555 3556 3557 3558
            }
        }
    }
    collect_mod(&mut names, module);

3559
    if names.is_empty() {
3560 3561 3562 3563 3564
        return "???".to_string();
    }
    names_to_string(&names.into_iter().rev().collect::<Vec<ast::Name>>())
}

3565 3566
fn err_path_resolution() -> PathResolution {
    PathResolution {
3567
        base_def: Def::Err,
3568 3569 3570 3571 3572
        last_private: LastMod(AllPublic),
        depth: 0,
    }
}

3573

3574
pub struct CrateMap {
J
Jonathan S 已提交
3575
    pub def_map: RefCell<DefMap>,
3576
    pub freevars: FreevarMap,
3577
    pub export_map: ExportMap,
3578 3579
    pub trait_map: TraitMap,
    pub external_exports: ExternalExports,
C
corentih 已提交
3580
    pub glob_map: Option<GlobMap>,
3581 3582
}

N
Niko Matsakis 已提交
3583
#[derive(PartialEq,Copy, Clone)]
3584 3585
pub enum MakeGlobMap {
    Yes,
C
corentih 已提交
3586
    No,
3587 3588
}

3589
/// Entry point to crate resolution.
3590
pub fn resolve_crate<'a, 'tcx>(session: &'a Session,
3591
                               ast_map: &'a hir_map::Map<'tcx>,
3592 3593
                               make_glob_map: MakeGlobMap)
                               -> CrateMap {
3594 3595 3596 3597 3598 3599 3600 3601 3602
    // Currently, we ignore the name resolution data structures for
    // the purposes of dependency tracking. Instead we will run name
    // resolution and include its output in the hash of each item,
    // much like we do for macro expansion. In other words, the hash
    // reflects not just its contents but the results of name
    // resolution on those contents. Hopefully we'll push this back at
    // some point.
    let _task = ast_map.dep_graph.in_task(DepNode::Resolve);

3603
    let krate = ast_map.krate();
3604 3605
    let arenas = Resolver::arenas();
    let mut resolver = create_resolver(session, ast_map, krate, make_glob_map, &arenas, None);
3606 3607 3608 3609 3610

    resolver.resolve_crate(krate);

    check_unused::check_crate(&mut resolver, krate);

3611
    CrateMap {
3612 3613
        def_map: resolver.def_map,
        freevars: resolver.freevars,
3614
        export_map: resolver.export_map,
3615 3616
        trait_map: resolver.trait_map,
        external_exports: resolver.external_exports,
3617
        glob_map: if resolver.make_glob_map {
C
corentih 已提交
3618 3619 3620 3621
            Some(resolver.glob_map)
        } else {
            None
        },
3622
    }
3623
}
3624

3625 3626 3627 3628 3629 3630 3631 3632
/// Builds a name resolution walker to be used within this module,
/// or used externally, with an optional callback function.
///
/// The callback takes a &mut bool which allows callbacks to end a
/// walk when set to true, passing through the rest of the walk, while
/// preserving the ribs + current module. This allows resolve_path
/// calls to be made with the correct scope info. The node in the
/// callback corresponds to the current node in the walk.
G
Garming Sam 已提交
3633
pub fn create_resolver<'a, 'tcx>(session: &'a Session,
3634
                                 ast_map: &'a hir_map::Map<'tcx>,
G
Garming Sam 已提交
3635 3636
                                 krate: &'a Crate,
                                 make_glob_map: MakeGlobMap,
3637
                                 arenas: &'a ResolverArenas<'a>,
3638
                                 callback: Option<Box<Fn(hir_map::Node, &mut bool) -> bool>>)
G
Garming Sam 已提交
3639
                                 -> Resolver<'a, 'tcx> {
3640
    let mut resolver = Resolver::new(session, ast_map, make_glob_map, arenas);
G
Garming Sam 已提交
3641 3642 3643 3644 3645 3646 3647 3648 3649 3650

    resolver.callback = callback;

    build_reduced_graph::build_reduced_graph(&mut resolver, krate);

    resolve_imports::resolve_imports(&mut resolver);

    resolver
}

3651
__build_diagnostic_array! { librustc_resolve, DIAGNOSTICS }