Skip to content

Commit 707f7a1

Browse files
author
Jorge Aparicio
committed
Check that traits with default impls have no methods
fixes #23080
1 parent 1fe8f22 commit 707f7a1

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,29 @@ 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+
468491
pub fn check_item_types(ccx: &CrateCtxt) {
469492
let krate = ccx.tcx.map.krate();
470493
let mut visit = wf::CheckTypeWellFormedVisitor::new(ccx);
@@ -478,6 +501,11 @@ pub fn check_item_types(ccx: &CrateCtxt) {
478501
visit::walk_crate(&mut visit, krate);
479502

480503
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();
481509
}
482510

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

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

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+
#![feature(optin_builtin_traits)]
12+
13+
unsafe trait Trait {
14+
//~^ error: traits with default impls (`e.g. impl Trait for ..`) must have no methods
15+
fn method(&self) {
16+
println!("Hello");
17+
}
18+
}
19+
20+
unsafe impl Trait for .. {}
21+
22+
fn call_method<T: Trait>(x: T) {
23+
x.method();
24+
}
25+
26+
fn main() {
27+
// ICE
28+
call_method(());
29+
}

0 commit comments

Comments
 (0)