Skip to content

Commit 7bf54f9

Browse files
authored
Auto merge of #35116 - jseyfried:groundwork_for_new_import_semantics, r=nrc
resolve: diagnostics improvement and groundwork for RFC 1560 Fixes #35115, fixes #35135, and lays groundwork for #32213 (cc #35120). r? @nrc
2 parents b30eff7 + de5eaab commit 7bf54f9

File tree

8 files changed

+223
-218
lines changed

8 files changed

+223
-218
lines changed

src/librustc/traits/object_safety.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
//! - not reference the erased type `Self` except for in this receiver;
1818
//! - not have generic type parameters
1919
20-
use super::supertraits;
2120
use super::elaborate_predicates;
2221

2322
use hir::def_id::DefId;

src/librustc_resolve/build_reduced_graph.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
1717
use Module;
1818
use Namespace::{self, TypeNS, ValueNS};
19-
use {NameBinding, NameBindingKind};
19+
use {NameBinding, NameBindingKind, ToNameBinding};
2020
use ParentLink::{ModuleParentLink, BlockParentLink};
2121
use Resolver;
2222
use {resolve_error, resolve_struct_error, ResolutionError};
@@ -39,10 +39,6 @@ use syntax::visit::{self, Visitor};
3939

4040
use syntax_pos::{Span, DUMMY_SP};
4141

42-
trait ToNameBinding<'a> {
43-
fn to_name_binding(self) -> NameBinding<'a>;
44-
}
45-
4642
impl<'a> ToNameBinding<'a> for (Module<'a>, Span, ty::Visibility) {
4743
fn to_name_binding(self) -> NameBinding<'a> {
4844
NameBinding { kind: NameBindingKind::Module(self.0), span: self.1, vis: self.2 }
@@ -68,18 +64,13 @@ impl<'b> Resolver<'b> {
6864
visit::walk_crate(&mut visitor, krate);
6965
}
7066

71-
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined.
72-
fn try_define<T>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
73-
where T: ToNameBinding<'b>
74-
{
75-
let _ = parent.try_define_child(name, ns, def.to_name_binding());
76-
}
77-
7867
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
7968
/// otherwise, reports an error.
80-
fn define<T: ToNameBinding<'b>>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) {
69+
fn define<T>(&mut self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
70+
where T: ToNameBinding<'b>,
71+
{
8172
let binding = def.to_name_binding();
82-
if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) {
73+
if let Err(old_binding) = self.try_define(parent, name, ns, binding.clone()) {
8374
self.report_conflict(parent, name, ns, old_binding, &binding);
8475
}
8576
}
@@ -399,14 +390,14 @@ impl<'b> Resolver<'b> {
399390
name, vis);
400391
let parent_link = ModuleParentLink(parent, name);
401392
let module = self.new_module(parent_link, Some(def), true);
402-
self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
393+
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
403394
}
404395
Def::Variant(_, variant_id) => {
405396
debug!("(building reduced graph for external crate) building variant {}", name);
406397
// Variants are always treated as importable to allow them to be glob used.
407398
// All variants are defined in both type and value namespaces as future-proofing.
408-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
409-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
399+
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
400+
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
410401
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
411402
// Not adding fields for variants as they are not accessed with a self receiver
412403
self.structs.insert(variant_id, Vec::new());
@@ -419,7 +410,7 @@ impl<'b> Resolver<'b> {
419410
Def::Method(..) => {
420411
debug!("(building reduced graph for external crate) building value (fn/static) {}",
421412
name);
422-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
413+
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
423414
}
424415
Def::Trait(def_id) => {
425416
debug!("(building reduced graph for external crate) building type {}", name);
@@ -441,20 +432,20 @@ impl<'b> Resolver<'b> {
441432

442433
let parent_link = ModuleParentLink(parent, name);
443434
let module = self.new_module(parent_link, Some(def), true);
444-
self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
435+
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
445436
}
446437
Def::TyAlias(..) | Def::AssociatedTy(..) => {
447438
debug!("(building reduced graph for external crate) building type {}", name);
448-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
439+
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
449440
}
450441
Def::Struct(def_id)
451442
if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => {
452443
debug!("(building reduced graph for external crate) building type and value for {}",
453444
name);
454-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
445+
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
455446
if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) {
456447
let def = Def::Struct(ctor_def_id);
457-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
448+
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
458449
}
459450

460451
// Record the def ID and fields of this struct.

src/librustc_resolve/lib.rs

+52-50
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ enum ResolutionError<'a> {
158158
/// error E0435: attempt to use a non-constant value in a constant
159159
AttemptToUseNonConstantValueInConstant,
160160
/// error E0530: X bindings cannot shadow Ys
161-
BindingShadowsSomethingUnacceptable(&'a str, &'a str, Name),
161+
BindingShadowsSomethingUnacceptable(&'a str, Name, &'a NameBinding<'a>),
162162
/// error E0531: unresolved pattern path kind `name`
163163
PatPathUnresolved(&'a str, &'a Path),
164164
/// error E0532: expected pattern path kind, found another pattern path kind
@@ -428,17 +428,16 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
428428
E0435,
429429
"attempt to use a non-constant value in a constant")
430430
}
431-
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, shadows_what, name) => {
431+
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {
432+
let shadows_what = PathResolution::new(binding.def().unwrap()).kind_name();
432433
let mut err = struct_span_err!(resolver.session,
433434
span,
434435
E0530,
435436
"{}s cannot shadow {}s", what_binding, shadows_what);
436437
err.span_label(span, &format!("cannot be named the same as a {}", shadows_what));
437-
if let Success(binding) = resolver.current_module.resolve_name(name, ValueNS, true) {
438-
let participle = if binding.is_import() { "imported" } else { "defined" };
439-
err.span_label(binding.span, &format!("a {} `{}` is {} here",
440-
shadows_what, name, participle));
441-
}
438+
let participle = if binding.is_import() { "imported" } else { "defined" };
439+
let msg = &format!("a {} `{}` is {} here", shadows_what, name, participle);
440+
err.span_label(binding.span, msg);
442441
err
443442
}
444443
ResolutionError::PatPathUnresolved(expected_what, path) => {
@@ -718,12 +717,16 @@ impl<'a> LexicalScopeBinding<'a> {
718717
}
719718
}
720719

721-
fn module(self) -> Option<Module<'a>> {
720+
fn item(self) -> Option<&'a NameBinding<'a>> {
722721
match self {
723-
LexicalScopeBinding::Item(binding) => binding.module(),
722+
LexicalScopeBinding::Item(binding) => Some(binding),
724723
_ => None,
725724
}
726725
}
726+
727+
fn module(self) -> Option<Module<'a>> {
728+
self.item().and_then(NameBinding::module)
729+
}
727730
}
728731

729732
/// The link from a module up to its nearest parent node.
@@ -824,6 +827,16 @@ pub struct NameBinding<'a> {
824827
vis: ty::Visibility,
825828
}
826829

830+
pub trait ToNameBinding<'a> {
831+
fn to_name_binding(self) -> NameBinding<'a>;
832+
}
833+
834+
impl<'a> ToNameBinding<'a> for NameBinding<'a> {
835+
fn to_name_binding(self) -> NameBinding<'a> {
836+
self
837+
}
838+
}
839+
827840
#[derive(Clone, Debug)]
828841
enum NameBindingKind<'a> {
829842
Def(Def),
@@ -1203,34 +1216,27 @@ impl<'a> Resolver<'a> {
12031216
match ns { ValueNS => &mut self.value_ribs, TypeNS => &mut self.type_ribs }
12041217
}
12051218

1206-
#[inline]
1207-
fn record_use(&mut self, name: Name, binding: &'a NameBinding<'a>) {
1219+
fn record_use(&mut self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>) {
12081220
// track extern crates for unused_extern_crate lint
12091221
if let Some(DefId { krate, .. }) = binding.module().and_then(ModuleS::def_id) {
12101222
self.used_crates.insert(krate);
12111223
}
12121224

1213-
let directive = match binding.kind {
1214-
NameBindingKind::Import { directive, .. } => directive,
1215-
_ => return,
1216-
};
1217-
1218-
if !self.make_glob_map {
1219-
return;
1220-
}
1221-
if self.glob_map.contains_key(&directive.id) {
1222-
self.glob_map.get_mut(&directive.id).unwrap().insert(name);
1223-
return;
1225+
if let NameBindingKind::Import { directive, .. } = binding.kind {
1226+
self.used_imports.insert((directive.id, ns));
1227+
self.add_to_glob_map(directive.id, name);
12241228
}
1229+
}
12251230

1226-
let mut new_set = FnvHashSet();
1227-
new_set.insert(name);
1228-
self.glob_map.insert(directive.id, new_set);
1231+
fn add_to_glob_map(&mut self, id: NodeId, name: Name) {
1232+
if self.make_glob_map {
1233+
self.glob_map.entry(id).or_insert_with(FnvHashSet).insert(name);
1234+
}
12291235
}
12301236

1231-
/// Resolves the given module path from the given root `module_`.
1237+
/// Resolves the given module path from the given root `search_module`.
12321238
fn resolve_module_path_from_root(&mut self,
1233-
module_: Module<'a>,
1239+
mut search_module: Module<'a>,
12341240
module_path: &[Name],
12351241
index: usize,
12361242
span: Span)
@@ -1247,7 +1253,6 @@ impl<'a> Resolver<'a> {
12471253
}
12481254
}
12491255

1250-
let mut search_module = module_;
12511256
let mut index = index;
12521257
let module_path_len = module_path.len();
12531258

@@ -1444,31 +1449,30 @@ impl<'a> Resolver<'a> {
14441449
}
14451450

14461451
/// Returns the nearest normal module parent of the given module.
1447-
fn get_nearest_normal_module_parent(&self, module_: Module<'a>) -> Option<Module<'a>> {
1448-
let mut module_ = module_;
1452+
fn get_nearest_normal_module_parent(&self, mut module: Module<'a>) -> Option<Module<'a>> {
14491453
loop {
1450-
match module_.parent_link {
1454+
match module.parent_link {
14511455
NoParentLink => return None,
14521456
ModuleParentLink(new_module, _) |
14531457
BlockParentLink(new_module, _) => {
14541458
let new_module = new_module;
14551459
if new_module.is_normal() {
14561460
return Some(new_module);
14571461
}
1458-
module_ = new_module;
1462+
module = new_module;
14591463
}
14601464
}
14611465
}
14621466
}
14631467

14641468
/// Returns the nearest normal module parent of the given module, or the
14651469
/// module itself if it is a normal module.
1466-
fn get_nearest_normal_module_parent_or_self(&self, module_: Module<'a>) -> Module<'a> {
1467-
if module_.is_normal() {
1468-
return module_;
1470+
fn get_nearest_normal_module_parent_or_self(&self, module: Module<'a>) -> Module<'a> {
1471+
if module.is_normal() {
1472+
return module;
14691473
}
1470-
match self.get_nearest_normal_module_parent(module_) {
1471-
None => module_,
1474+
match self.get_nearest_normal_module_parent(module) {
1475+
None => module,
14721476
Some(new_module) => new_module,
14731477
}
14741478
}
@@ -1485,8 +1489,8 @@ impl<'a> Resolver<'a> {
14851489
"super" => 0,
14861490
_ => return Success(NoPrefixFound),
14871491
};
1488-
let module_ = self.current_module;
1489-
let mut containing_module = self.get_nearest_normal_module_parent_or_self(module_);
1492+
let mut containing_module =
1493+
self.get_nearest_normal_module_parent_or_self(self.current_module);
14901494

14911495
// Now loop through all the `super`s we find.
14921496
while i < module_path.len() && "super" == module_path[i].as_str() {
@@ -1525,10 +1529,7 @@ impl<'a> Resolver<'a> {
15251529
self.populate_module_if_necessary(module);
15261530
module.resolve_name(name, namespace, use_lexical_scope).and_then(|binding| {
15271531
if record_used {
1528-
if let NameBindingKind::Import { directive, .. } = binding.kind {
1529-
self.used_imports.insert((directive.id, namespace));
1530-
}
1531-
self.record_use(name, binding);
1532+
self.record_use(name, namespace, binding);
15321533
}
15331534
Success(binding)
15341535
})
@@ -2308,16 +2309,17 @@ impl<'a> Resolver<'a> {
23082309
PatKind::Ident(bmode, ref ident, ref opt_pat) => {
23092310
// First try to resolve the identifier as some existing
23102311
// entity, then fall back to a fresh binding.
2311-
let resolution = self.resolve_identifier(ident.node, ValueNS, true)
2312-
.map(|local_def| PathResolution::new(local_def.def))
2313-
.and_then(|resolution| {
2312+
let binding = self.resolve_ident_in_lexical_scope(ident.node, ValueNS, false)
2313+
.and_then(LexicalScopeBinding::item);
2314+
let resolution = binding.and_then(NameBinding::def).and_then(|def| {
23142315
let always_binding = !pat_src.is_refutable() || opt_pat.is_some() ||
23152316
bmode != BindingMode::ByValue(Mutability::Immutable);
2316-
match resolution.base_def {
2317+
match def {
23172318
Def::Struct(..) | Def::Variant(..) |
23182319
Def::Const(..) | Def::AssociatedConst(..) if !always_binding => {
23192320
// A constant, unit variant, etc pattern.
2320-
Some(resolution)
2321+
self.record_use(ident.node.name, ValueNS, binding.unwrap());
2322+
Some(PathResolution::new(def))
23212323
}
23222324
Def::Struct(..) | Def::Variant(..) |
23232325
Def::Const(..) | Def::AssociatedConst(..) | Def::Static(..) => {
@@ -2326,7 +2328,7 @@ impl<'a> Resolver<'a> {
23262328
self,
23272329
ident.span,
23282330
ResolutionError::BindingShadowsSomethingUnacceptable(
2329-
pat_src.descr(), resolution.kind_name(), ident.node.name)
2331+
pat_src.descr(), ident.node.name, binding.unwrap())
23302332
);
23312333
None
23322334
}
@@ -3136,10 +3138,10 @@ impl<'a> Resolver<'a> {
31363138
if let NameBindingKind::Import { directive, .. } = binding.kind {
31373139
let id = directive.id;
31383140
this.maybe_unused_trait_imports.insert(id);
3141+
this.add_to_glob_map(id, trait_name);
31393142
import_id = Some(id);
31403143
}
31413144
add_trait_info(&mut found_traits, trait_def_id, import_id, name);
3142-
this.record_use(trait_name, binding);
31433145
}
31443146
}
31453147
};

0 commit comments

Comments
 (0)