Skip to content

Commit 8a391dd

Browse files
author
Jorge Aparicio
committed
move check into wf pass, add a test for assoc types
1 parent 707f7a1 commit 8a391dd

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -465,29 +465,6 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckItemTypesVisitor<'a, 'tcx> {
465465
}
466466
}
467467

468-
// Check that trait with default impls (`impl Trait for ..`) contain no methods
469-
struct DefaultedTraitVisitor<'a, 'tcx: 'a> { ccx: &'a CrateCtxt<'a, 'tcx> }
470-
471-
impl<'a, 'tcx> Visitor<'tcx> for DefaultedTraitVisitor<'a, 'tcx> {
472-
fn visit_item(&mut self, item: &ast::Item) {
473-
let tcx = self.ccx.tcx;
474-
475-
match item.node {
476-
ast::ItemTrait(_, _, _, ref trait_methods) => {
477-
if ty::trait_has_default_impl(tcx, local_def(item.id)) &&
478-
!trait_methods.is_empty()
479-
{
480-
tcx.sess.span_err(
481-
item.span,
482-
"traits with default impls (`e.g. impl Trait for ..`) must have no \
483-
methods")
484-
}
485-
},
486-
_ => {},
487-
}
488-
}
489-
}
490-
491468
pub fn check_item_types(ccx: &CrateCtxt) {
492469
let krate = ccx.tcx.map.krate();
493470
let mut visit = wf::CheckTypeWellFormedVisitor::new(ccx);
@@ -501,11 +478,6 @@ pub fn check_item_types(ccx: &CrateCtxt) {
501478
visit::walk_crate(&mut visit, krate);
502479

503480
ccx.tcx.sess.abort_if_errors();
504-
505-
let mut visit = DefaultedTraitVisitor { ccx: ccx };
506-
visit::walk_crate(&mut visit, krate);
507-
508-
ccx.tcx.sess.abort_if_errors();
509481
}
510482

511483
fn check_bare_fn<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,

src/librustc_typeck/check/wf.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
118118

119119
self.check_variances_for_type_defn(item, ast_generics);
120120
}
121-
ast::ItemTrait(_, ref ast_generics, _, _) => {
121+
ast::ItemTrait(_, ref ast_generics, _, ref items) => {
122122
let trait_predicates =
123123
ty::lookup_predicates(ccx.tcx, local_def(item.id));
124124
reject_non_type_param_bounds(
@@ -127,6 +127,14 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
127127
&trait_predicates);
128128
self.check_variances(item, ast_generics, &trait_predicates,
129129
self.tcx().lang_items.phantom_fn());
130+
if ty::trait_has_default_impl(ccx.tcx, local_def(item.id)) {
131+
if !items.is_empty() {
132+
ccx.tcx.sess.span_err(
133+
item.span,
134+
"traits with default impls (`e.g. unsafe impl Trait for ..`) must \
135+
have no methods or associated items")
136+
}
137+
}
130138
}
131139
_ => {}
132140
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// ignore-tidy-linelength
12+
13+
#![feature(optin_builtin_traits)]
14+
15+
use std::marker::MarkerTrait;
16+
17+
unsafe trait Trait: MarkerTrait {
18+
//~^ error: traits with default impls (`e.g. unsafe impl Trait for ..`) must have no methods or associated items
19+
type Output;
20+
}
21+
22+
unsafe impl Trait for .. {}
23+
24+
fn call_method<T: Trait>(x: T) {}
25+
26+
fn main() {
27+
// ICE
28+
call_method(());
29+
}

src/test/compile-fail/issue-23080.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// ignore-tidy-linelength
12+
1113
#![feature(optin_builtin_traits)]
1214

1315
unsafe trait Trait {
14-
//~^ error: traits with default impls (`e.g. impl Trait for ..`) must have no methods
16+
//~^ error: traits with default impls (`e.g. unsafe impl Trait for ..`) must have no methods or associated items
1517
fn method(&self) {
1618
println!("Hello");
1719
}

0 commit comments

Comments
 (0)