show each object-safety violation once

different supertraits can suffer from the same object-safety violation,
leading to duplication in the error message. Avoid it.

Fixes #20692
This commit is contained in:
Ariel Ben-Yehuda 2015-09-25 02:40:57 +03:00 committed by Brian Anderson
parent eb2aca307f
commit fc166d44f0
4 changed files with 45 additions and 3 deletions

View file

@ -28,7 +28,8 @@ use middle::def_id::DefId;
use middle::infer::InferCtxt;
use middle::ty::{self, ToPredicate, HasTypeFlags, ToPolyTraitRef, TraitRef, Ty};
use middle::ty::fold::TypeFoldable;
use std::collections::HashMap;
use util::nodemap::{FnvHashMap, FnvHashSet};
use std::fmt;
use syntax::codemap::Span;
use rustc_front::attr::{AttributeMethods, AttrMetaMethods};
@ -124,7 +125,7 @@ fn report_on_unimplemented<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
(gen.name.as_str().to_string(),
trait_ref.substs.types.get(param, i)
.to_string())
}).collect::<HashMap<String, String>>();
}).collect::<FnvHashMap<String, String>>();
generic_map.insert("Self".to_string(),
trait_ref.self_ty().to_string());
let parser = Parser::new(&istring);
@ -329,7 +330,11 @@ pub fn report_object_safety_error<'tcx>(tcx: &ty::ctxt<'tcx>,
"the trait `{}` cannot be made into an object",
tcx.item_path_str(trait_def_id));
let mut reported_violations = FnvHashSet();
for violation in object_safety_violations(tcx, trait_def_id) {
if !reported_violations.insert(violation.clone()) {
continue;
}
match violation {
ObjectSafetyViolation::SizedSelf => {
tcx.sess.fileline_note(

View file

@ -27,7 +27,7 @@ use middle::ty::{self, ToPolyTraitRef, Ty};
use std::rc::Rc;
use syntax::ast;
#[derive(Debug)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ObjectSafetyViolation<'tcx> {
/// Self : Sized declared on the trait
SizedSelf,

View file

@ -272,6 +272,20 @@ impl<'tcx> Method<'tcx> {
}
}
impl<'tcx> PartialEq for Method<'tcx> {
#[inline]
fn eq(&self, other: &Self) -> bool { self.def_id == other.def_id }
}
impl<'tcx> Eq for Method<'tcx> {}
impl<'tcx> Hash for Method<'tcx> {
#[inline]
fn hash<H: Hasher>(&self, s: &mut H) {
self.def_id.hash(s)
}
}
#[derive(Clone, Copy, Debug)]
pub struct AssociatedConst<'tcx> {
pub name: Name,

View file

@ -0,0 +1,23 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
trait Array: Sized {}
fn f<T: Array>(x: &T) {
let _ = x
//~^ ERROR `Array` cannot be made into an object
//~| NOTE the trait cannot require that `Self : Sized`
as
&Array;
//~^ ERROR `Array` cannot be made into an object
//~| NOTE the trait cannot require that `Self : Sized`
}
fn main() {}