Skip to content

Commit ef97813

Browse files
committed
Auto merge of #52555 - petrochenkov:mresfact, r=alexcrichton
resolve: Some renaming, refactoring and comments Commits are self-descriptive. The only functional change is 34bf2f5 that tightens shadowing rules for macro paths (makes the second and third cases in `test/ui/imports/glob-shadowing.rs` an error).
2 parents bd455ef + 382285a commit ef97813

File tree

12 files changed

+246
-164
lines changed

12 files changed

+246
-164
lines changed

src/liballoc/collections/btree/node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<K, V> LeafNode<K, V> {
103103
}
104104

105105
fn is_shared_root(&self) -> bool {
106-
self as *const _ == &EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V>
106+
ptr::eq(self, &EMPTY_ROOT_NODE as *const _ as *const _)
107107
}
108108
}
109109

src/librustc/lint/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use hir::intravisit;
3939
use hir;
4040
use lint::builtin::BuiltinLintDiagnostics;
4141
use session::{Session, DiagnosticMessageId};
42-
use std::hash;
42+
use std::{hash, ptr};
4343
use syntax::ast;
4444
use syntax::codemap::{MultiSpan, ExpnFormat};
4545
use syntax::edition::Edition;
@@ -354,7 +354,7 @@ pub struct LintId {
354354

355355
impl PartialEq for LintId {
356356
fn eq(&self, other: &LintId) -> bool {
357-
(self.lint as *const Lint) == (other.lint as *const Lint)
357+
ptr::eq(self.lint, other.lint)
358358
}
359359
}
360360

src/librustc/ty/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use std::ops::Deref;
4747
use rustc_data_structures::sync::{self, Lrc, ParallelIterator, par_iter};
4848
use std::slice;
4949
use std::vec::IntoIter;
50-
use std::mem;
50+
use std::{mem, ptr};
5151
use syntax::ast::{self, DUMMY_NODE_ID, Name, Ident, NodeId};
5252
use syntax::attr;
5353
use syntax::ext::hygiene::Mark;
@@ -527,8 +527,7 @@ impl<'tcx> PartialOrd for TyS<'tcx> {
527527
impl<'tcx> PartialEq for TyS<'tcx> {
528528
#[inline]
529529
fn eq(&self, other: &TyS<'tcx>) -> bool {
530-
// (self as *const _) == (other as *const _)
531-
(self as *const TyS<'tcx>) == (other as *const TyS<'tcx>)
530+
ptr::eq(self, other)
532531
}
533532
}
534533
impl<'tcx> Eq for TyS<'tcx> {}
@@ -678,7 +677,7 @@ impl<T> PartialOrd for Slice<T> where T: PartialOrd {
678677
impl<T: PartialEq> PartialEq for Slice<T> {
679678
#[inline]
680679
fn eq(&self, other: &Slice<T>) -> bool {
681-
(self as *const _) == (other as *const _)
680+
ptr::eq(self, other)
682681
}
683682
}
684683
impl<T: Eq> Eq for Slice<T> {}
@@ -1730,7 +1729,7 @@ impl Ord for AdtDef {
17301729
impl PartialEq for AdtDef {
17311730
// AdtDef are always interned and this is part of TyS equality
17321731
#[inline]
1733-
fn eq(&self, other: &Self) -> bool { self as *const _ == other as *const _ }
1732+
fn eq(&self, other: &Self) -> bool { ptr::eq(self, other) }
17341733
}
17351734

17361735
impl Eq for AdtDef {}

src/librustc/ty/query/job.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ty::query::plumbing::CycleError;
2020
use ty::context::TyCtxt;
2121
use errors::Diagnostic;
2222
use std::process;
23-
use std::fmt;
23+
use std::{fmt, ptr};
2424
use std::collections::HashSet;
2525
#[cfg(parallel_queries)]
2626
use {
@@ -124,7 +124,7 @@ impl<'tcx> QueryJob<'tcx> {
124124
while let Some(job) = current_job {
125125
cycle.insert(0, job.info.clone());
126126

127-
if &*job as *const _ == self as *const _ {
127+
if ptr::eq(&*job, self) {
128128
// This is the end of the cycle
129129
// The span entry we included was for the usage
130130
// of the cycle itself, and not part of the cycle

src/librustc_data_structures/lib.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,30 @@ extern crate rustc_cratesio_shim;
5656

5757
pub use rustc_serialize::hex::ToHex;
5858

59-
pub mod array_vec;
6059
pub mod accumulate_vec;
61-
pub mod small_vec;
60+
pub mod array_vec;
6261
pub mod base_n;
6362
pub mod bitslice;
6463
pub mod bitvec;
64+
pub mod flock;
65+
pub mod fx;
66+
pub mod graph;
6567
pub mod indexed_set;
6668
pub mod indexed_vec;
6769
pub mod obligation_forest;
70+
pub mod owning_ref;
71+
pub mod ptr_key;
6872
pub mod sip128;
73+
pub mod small_vec;
6974
pub mod snapshot_map;
7075
pub use ena::snapshot_vec;
76+
pub mod sorted_map;
7177
pub mod stable_hasher;
72-
pub mod transitive_relation;
73-
pub use ena::unify;
74-
pub mod fx;
75-
pub mod tuple_slice;
76-
pub mod graph;
77-
pub mod flock;
7878
pub mod sync;
79-
pub mod owning_ref;
8079
pub mod tiny_list;
81-
pub mod sorted_map;
80+
pub mod transitive_relation;
81+
pub mod tuple_slice;
82+
pub use ena::unify;
8283
pub mod work_queue;
8384

8485
pub struct OnDrop<F: Fn()>(pub F);
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::{hash, ptr};
12+
use std::ops::Deref;
13+
14+
/// A wrapper around reference that compares and hashes like a pointer.
15+
/// Can be used as a key in sets/maps indexed by pointers to avoid `unsafe`.
16+
#[derive(Debug)]
17+
pub struct PtrKey<'a, T: 'a>(pub &'a T);
18+
19+
impl<'a, T> Clone for PtrKey<'a, T> {
20+
fn clone(&self) -> Self { *self }
21+
}
22+
23+
impl<'a, T> Copy for PtrKey<'a, T> {}
24+
25+
impl<'a, T> PartialEq for PtrKey<'a, T> {
26+
fn eq(&self, rhs: &Self) -> bool {
27+
ptr::eq(self.0, rhs.0)
28+
}
29+
}
30+
31+
impl<'a, T> Eq for PtrKey<'a, T> {}
32+
33+
impl<'a, T> hash::Hash for PtrKey<'a, T> {
34+
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
35+
(self.0 as *const T).hash(hasher)
36+
}
37+
}
38+
39+
impl<'a, T> Deref for PtrKey<'a, T> {
40+
type Target = T;
41+
42+
fn deref(&self) -> &Self::Target {
43+
self.0
44+
}
45+
}

src/librustc_resolve/build_reduced_graph.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl<'a> Resolver<'a> {
651651
binding: &'a NameBinding<'a>,
652652
span: Span,
653653
allow_shadowing: bool) {
654-
if self.global_macros.insert(name, binding).is_some() && !allow_shadowing {
654+
if self.macro_prelude.insert(name, binding).is_some() && !allow_shadowing {
655655
let msg = format!("`{}` is already in scope", name);
656656
let note =
657657
"macro-expanded `#[macro_use]`s may not shadow existing macros (see RFC 1560)";
@@ -704,8 +704,7 @@ impl<'a> Resolver<'a> {
704704
} else {
705705
for (name, span) in legacy_imports.imports {
706706
let ident = Ident::with_empty_ctxt(name);
707-
let result = self.resolve_ident_in_module(module, ident, MacroNS,
708-
false, false, span);
707+
let result = self.resolve_ident_in_module(module, ident, MacroNS, false, span);
709708
if let Ok(binding) = result {
710709
let directive = macro_use_directive(span);
711710
self.potentially_unused_imports.push(directive);

src/librustc_resolve/lib.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ pub struct Resolver<'a> {
13931393

13941394
crate_loader: &'a mut dyn CrateLoader,
13951395
macro_names: FxHashSet<Ident>,
1396-
global_macros: FxHashMap<Name, &'a NameBinding<'a>>,
1396+
macro_prelude: FxHashMap<Name, &'a NameBinding<'a>>,
13971397
pub all_macros: FxHashMap<Name, Def>,
13981398
lexical_macro_resolutions: Vec<(Ident, &'a Cell<LegacyScope<'a>>)>,
13991399
macro_map: FxHashMap<DefId, Lrc<SyntaxExtension>>,
@@ -1715,7 +1715,7 @@ impl<'a> Resolver<'a> {
17151715

17161716
crate_loader,
17171717
macro_names: FxHashSet(),
1718-
global_macros: FxHashMap(),
1718+
macro_prelude: FxHashMap(),
17191719
all_macros: FxHashMap(),
17201720
lexical_macro_resolutions: Vec::new(),
17211721
macro_map: FxHashMap(),
@@ -2002,7 +2002,6 @@ impl<'a> Resolver<'a> {
20022002
module: Module<'a>,
20032003
mut ident: Ident,
20042004
ns: Namespace,
2005-
ignore_unresolved_invocations: bool,
20062005
record_used: bool,
20072006
span: Span)
20082007
-> Result<&'a NameBinding<'a>, Determinacy> {
@@ -2012,7 +2011,7 @@ impl<'a> Resolver<'a> {
20122011
self.current_module = self.macro_def_scope(def);
20132012
}
20142013
let result = self.resolve_ident_in_module_unadjusted(
2015-
module, ident, ns, ignore_unresolved_invocations, record_used, span,
2014+
module, ident, ns, false, record_used, span,
20162015
);
20172016
self.current_module = orig_current_module;
20182017
result
@@ -2518,7 +2517,7 @@ impl<'a> Resolver<'a> {
25182517
// If there is a TraitRef in scope for an impl, then the method must be in the
25192518
// trait.
25202519
if let Some((module, _)) = self.current_trait_ref {
2521-
if self.resolve_ident_in_module(module, ident, ns, false, false, span).is_err() {
2520+
if self.resolve_ident_in_module(module, ident, ns, false, span).is_err() {
25222521
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
25232522
resolve_error(self, span, err(ident.name, &path_names_to_string(path)));
25242523
}
@@ -3225,7 +3224,7 @@ impl<'a> Resolver<'a> {
32253224
};
32263225
}
32273226
}
3228-
let is_global = self.global_macros.get(&path[0].name).cloned()
3227+
let is_global = self.macro_prelude.get(&path[0].name).cloned()
32293228
.map(|binding| binding.get_macro(self).kind() == MacroKind::Bang).unwrap_or(false);
32303229
if primary_ns != MacroNS && (is_global ||
32313230
self.macro_names.contains(&path[0].modern())) {
@@ -3468,7 +3467,7 @@ impl<'a> Resolver<'a> {
34683467
}
34693468

34703469
let binding = if let Some(module) = module {
3471-
self.resolve_ident_in_module(module, ident, ns, false, record_used, path_span)
3470+
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
34723471
} else if opt_ns == Some(MacroNS) {
34733472
self.resolve_lexical_macro_path_segment(ident, ns, record_used, path_span)
34743473
.map(MacroBinding::binding)
@@ -3762,7 +3761,7 @@ impl<'a> Resolver<'a> {
37623761
// Look for associated items in the current trait.
37633762
if let Some((module, _)) = self.current_trait_ref {
37643763
if let Ok(binding) =
3765-
self.resolve_ident_in_module(module, ident, ns, false, false, module.span) {
3764+
self.resolve_ident_in_module(module, ident, ns, false, module.span) {
37663765
let def = binding.def();
37673766
if filter_fn(def) {
37683767
return Some(if self.has_self.contains(&def.def_id()) {
@@ -4075,7 +4074,7 @@ impl<'a> Resolver<'a> {
40754074
let mut found_traits = Vec::new();
40764075
// Look for the current trait.
40774076
if let Some((module, _)) = self.current_trait_ref {
4078-
if self.resolve_ident_in_module(module, ident, ns, false, false, module.span).is_ok() {
4077+
if self.resolve_ident_in_module(module, ident, ns, false, module.span).is_ok() {
40794078
let def_id = module.def_id().unwrap();
40804079
found_traits.push(TraitCandidate { def_id: def_id, import_id: None });
40814080
}

src/librustc_resolve/macros.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'a> base::Resolver for Resolver<'a> {
220220
vis: ty::Visibility::Invisible,
221221
expansion: Mark::root(),
222222
});
223-
self.global_macros.insert(ident.name, binding);
223+
self.macro_prelude.insert(ident.name, binding);
224224
}
225225

226226
fn resolve_imports(&mut self) {
@@ -238,7 +238,7 @@ impl<'a> base::Resolver for Resolver<'a> {
238238
attr::mark_known(&attrs[i]);
239239
}
240240

241-
match self.global_macros.get(&name).cloned() {
241+
match self.macro_prelude.get(&name).cloned() {
242242
Some(binding) => match *binding.get_macro(self) {
243243
MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
244244
return Some(attrs.remove(i))
@@ -274,7 +274,7 @@ impl<'a> base::Resolver for Resolver<'a> {
274274
}
275275
let trait_name = traits[j].segments[0].ident.name;
276276
let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
277-
if !self.global_macros.contains_key(&legacy_name) {
277+
if !self.macro_prelude.contains_key(&legacy_name) {
278278
continue
279279
}
280280
let span = traits.remove(j).span;
@@ -565,7 +565,7 @@ impl<'a> Resolver<'a> {
565565
module, ident, ns, true, record_used, path_span,
566566
).map(MacroBinding::Modern)
567567
} else {
568-
self.global_macros.get(&ident.name).cloned().ok_or(determinacy)
568+
self.macro_prelude.get(&ident.name).cloned().ok_or(determinacy)
569569
.map(MacroBinding::Global)
570570
};
571571
self.current_module = orig_current_module;
@@ -588,8 +588,7 @@ impl<'a> Resolver<'a> {
588588
return potential_illegal_shadower;
589589
}
590590
}
591-
if binding.expansion != Mark::root() ||
592-
(binding.is_glob_import() && module.unwrap().def().is_some()) {
591+
if binding.is_glob_import() || binding.expansion != Mark::root() {
593592
potential_illegal_shadower = result;
594593
} else {
595594
return result;
@@ -652,7 +651,7 @@ impl<'a> Resolver<'a> {
652651

653652
let binding = if let Some(binding) = binding {
654653
MacroBinding::Legacy(binding)
655-
} else if let Some(binding) = self.global_macros.get(&ident.name).cloned() {
654+
} else if let Some(binding) = self.macro_prelude.get(&ident.name).cloned() {
656655
if !self.use_extern_macros {
657656
self.record_use(ident, MacroNS, binding, DUMMY_SP);
658657
}
@@ -762,8 +761,8 @@ impl<'a> Resolver<'a> {
762761
// Then check global macros.
763762
}.or_else(|| {
764763
// FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
765-
let global_macros = self.global_macros.clone();
766-
let names = global_macros.iter().filter_map(|(name, binding)| {
764+
let macro_prelude = self.macro_prelude.clone();
765+
let names = macro_prelude.iter().filter_map(|(name, binding)| {
767766
if binding.get_macro(self).kind() == kind {
768767
Some(name)
769768
} else {

0 commit comments

Comments
 (0)