Skip to content

Commit 5b4d5ce

Browse files
committed
rustc: Make entire crates privileged scopes for the purposes of coherence
1 parent 3b554c1 commit 5b4d5ce

File tree

2 files changed

+9
-55
lines changed

2 files changed

+9
-55
lines changed

src/rustc/middle/typeck/coherence.rs

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,12 @@ struct CoherenceChecker {
147147

148148
let privileged_implementations: hashmap<node_id,()>;
149149

150-
// The set of types that we are currently in the privileged scope of. This
151-
// is used while we traverse the AST while checking privileged scopes.
152-
153-
let privileged_types: hashmap<def_id,()>;
154-
155150
new(crate_context: @crate_ctxt) {
156151
self.crate_context = crate_context;
157152
self.inference_context = new_infer_ctxt(crate_context.tcx);
158153

159154
self.base_type_def_ids = new_def_hash();
160155
self.privileged_implementations = int_hash();
161-
self.privileged_types = new_def_hash();
162156
}
163157

164158
// Create a mapping containing a MethodInfo for every provided
@@ -427,36 +421,12 @@ struct CoherenceChecker {
427421

428422
// Privileged scope checking
429423
fn check_privileged_scopes(crate: @crate) {
430-
// Gather up all privileged types.
431-
let privileged_types =
432-
self.gather_privileged_types(crate.node.module.items);
433-
for privileged_types.each |privileged_type| {
434-
self.privileged_types.insert(privileged_type, ());
435-
}
436-
437424
visit_crate(*crate, (), mk_vt(@{
438425
visit_item: |item, _context, visitor| {
439426
match item.node {
440427
item_mod(module_) => {
441-
// First, gather up all privileged types.
442-
let privileged_types =
443-
self.gather_privileged_types(module_.items);
444-
for privileged_types.each |privileged_type| {
445-
debug!("(checking privileged scopes) entering \
446-
privileged scope of %d:%d",
447-
privileged_type.crate,
448-
privileged_type.node);
449-
450-
self.privileged_types.insert(privileged_type, ());
451-
}
452-
453428
// Then visit the module items.
454429
visit_mod(module_, item.span, item.id, (), visitor);
455-
456-
// Finally, remove privileged types from the map.
457-
for privileged_types.each |privileged_type| {
458-
self.privileged_types.remove(privileged_type);
459-
}
460430
}
461431
item_impl(_, associated_traits, _, _) => {
462432
match self.base_type_def_ids.find(
@@ -467,12 +437,9 @@ struct CoherenceChecker {
467437
}
468438
Some(base_type_def_id) => {
469439
// Check to see whether the implementation is
470-
// in the scope of its base type.
471-
472-
let privileged_types = &self.privileged_types;
473-
if privileged_types.
474-
contains_key(base_type_def_id) {
440+
// in the same crate as its base type.
475441

442+
if base_type_def_id.crate == local_crate {
476443
// Record that this implementation is OK.
477444
self.privileged_implementations.insert
478445
(item.id, ());
@@ -492,7 +459,7 @@ struct CoherenceChecker {
492459
~"cannot implement \
493460
inherent methods \
494461
for a type outside \
495-
the scope the type \
462+
the crate the type \
496463
was defined in; \
497464
define and \
498465
implement a trait \
@@ -546,25 +513,6 @@ struct CoherenceChecker {
546513
return trait_id;
547514
}
548515
549-
fn gather_privileged_types(items: ~[@item]) -> @DVec<def_id> {
550-
let results = @DVec();
551-
for items.each |item| {
552-
match item.node {
553-
item_class(*) | item_enum(*) | item_trait(*) => {
554-
results.push(local_def(item.id));
555-
}
556-
557-
item_const(*) | item_fn(*) | item_mod(*) |
558-
item_foreign_mod(*) | item_ty(*) | item_impl(*) |
559-
item_mac(*) => {
560-
// Nothing to do.
561-
}
562-
}
563-
}
564-
565-
return results;
566-
}
567-
568516
// Converts an implementation in the AST to an Impl structure.
569517
fn create_impl_from_item(item: @item) -> @Impl {
570518
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
enum x { foo }
3+
impl x : core::cmp::Eq {
4+
pure fn eq(&&other: x) -> bool { self as int == other as int }
5+
}
6+
}

0 commit comments

Comments
 (0)