Skip to content

Commit 3343e9c

Browse files
nikomatsakisflaper87
authored andcommitted
Add new test for impl precedence and remove unnecessary coherence rules that prevent the test from compiling.
1 parent 6d1844c commit 3343e9c

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

src/librustc_typeck/coherence/overlap.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,12 @@ use middle::infer::{self, new_infer_ctxt};
1717
use syntax::ast::{DefId};
1818
use syntax::ast::{LOCAL_CRATE};
1919
use syntax::ast;
20-
use syntax::ast_util;
21-
use syntax::visit;
2220
use syntax::codemap::Span;
2321
use util::ppaux::Repr;
2422

2523
pub fn check(tcx: &ty::ctxt) {
26-
let mut overlap = OverlapChecker { tcx: tcx };
24+
let overlap = OverlapChecker { tcx: tcx };
2725
overlap.check_for_overlapping_impls();
28-
29-
// this secondary walk specifically checks for impls of defaulted
30-
// traits, for which additional overlap rules exist
31-
visit::walk_crate(&mut overlap, tcx.map.krate());
3226
}
3327

3428
struct OverlapChecker<'cx, 'tcx:'cx> {
@@ -128,52 +122,3 @@ impl<'cx, 'tcx> OverlapChecker<'cx, 'tcx> {
128122
self.tcx.map.span(impl_did.node)
129123
}
130124
}
131-
132-
133-
impl<'cx, 'tcx,'v> visit::Visitor<'v> for OverlapChecker<'cx, 'tcx> {
134-
fn visit_item(&mut self, item: &'v ast::Item) {
135-
match item.node {
136-
ast::ItemImpl(_, ast::ImplPolarity::Positive, _, Some(ref ast_trait_ref), _, _) => {
137-
let trait_ref = ty::node_id_to_trait_ref(self.tcx, ast_trait_ref.ref_id);
138-
match ty::trait_default_impl(self.tcx, trait_ref.def_id) {
139-
Some(default_impl) => {
140-
match trait_ref.self_ty().sty {
141-
ty::ty_struct(..) | ty::ty_enum(..) => {},
142-
_ => {
143-
let impl_def_id = ast_util::local_def(item.id);
144-
span_err!(self.tcx.sess, self.span_of_impl(impl_def_id), E0319,
145-
"implementations for traits providing default \
146-
implementations are only allowed on structs and enums");
147-
148-
self.report_overlap_note(impl_def_id, default_impl);
149-
}
150-
}
151-
}
152-
None => {}
153-
}
154-
}
155-
ast::ItemDefaultImpl(_, _) => {
156-
let impl_def_id = ast_util::local_def(item.id);
157-
match ty::impl_trait_ref(self.tcx, impl_def_id) {
158-
Some(ref trait_ref) => {
159-
match ty::trait_default_impl(self.tcx, trait_ref.def_id) {
160-
Some(other_impl) if other_impl != impl_def_id => {
161-
self.report_overlap_error(trait_ref.def_id,
162-
other_impl,
163-
impl_def_id);
164-
}
165-
Some(_) => {}
166-
None => {
167-
self.tcx.sess.bug(
168-
&format!("no default implementation recorded for `{:?}`",
169-
item)[]);
170-
}
171-
}
172-
}
173-
_ => {}
174-
}
175-
}
176-
_ => {}
177-
}
178-
}
179-
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2015 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+
// Test that declaring that `&T` is `Defaulted` if `T:Signed` implies
12+
// that other `&T` is NOT `Defaulted` if `T:Signed` does not hold. In
13+
// other words, the `..` impl only applies if there are no existing
14+
// impls whose types unify.
15+
16+
#![feature(optin_builtin_traits)]
17+
18+
use std::marker::MarkerTrait;
19+
20+
trait Defaulted : MarkerTrait { }
21+
impl Defaulted for .. { }
22+
impl<'a,T:Signed> Defaulted for &'a T { }
23+
impl<'a,T:Signed> Defaulted for &'a mut T { }
24+
fn is_defaulted<T:Defaulted>() { }
25+
26+
trait Signed : MarkerTrait { }
27+
impl Signed for i32 { }
28+
29+
fn main() {
30+
is_defaulted::<&'static i32>();
31+
is_defaulted::<&'static u32>();
32+
}

0 commit comments

Comments
 (0)