Skip to content

Commit a0a6190

Browse files
author
Alexander Regueiro
committed
Addressed more points raised in review.
1 parent 783b713 commit a0a6190

File tree

3 files changed

+38
-50
lines changed

3 files changed

+38
-50
lines changed

src/librustc/traits/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ pub use self::util::{elaborate_predicates, elaborate_trait_ref, elaborate_trait_
6363
pub use self::util::{
6464
supertraits, supertrait_def_ids, transitive_bounds, Supertraits, SupertraitDefIds,
6565
};
66-
pub use self::util::{
67-
expand_trait_aliases, TraitAliasExpander, TraitAliasExpansionInfoDignosticBuilder,
68-
};
66+
pub use self::util::{expand_trait_aliases, TraitAliasExpander};
6967

7068
pub use self::chalk_fulfill::{
7169
CanonicalGoal as ChalkCanonicalGoal,

src/librustc/traits/util.rs

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,18 @@ impl<'cx, 'gcx, 'tcx> Elaborator<'cx, 'gcx, 'tcx> {
132132
// Get predicates declared on the trait.
133133
let predicates = tcx.super_predicates_of(data.def_id());
134134

135-
let mut predicates: Vec<_> = predicates.predicates
135+
let predicates = predicates.predicates
136136
.iter()
137-
.map(|(pred, _)| pred.subst_supertrait(tcx, &data.to_poly_trait_ref()))
138-
.collect();
137+
.map(|(pred, _)| pred.subst_supertrait(tcx, &data.to_poly_trait_ref()));
139138
debug!("super_predicates: data={:?} predicates={:?}",
140-
data, predicates);
139+
data, predicates.clone());
141140

142141
// Only keep those bounds that we haven't already seen.
143142
// This is necessary to prevent infinite recursion in some
144143
// cases. One common case is when people define
145144
// `trait Sized: Sized { }` rather than `trait Sized { }`.
146-
predicates.retain(|pred| self.visited.insert(pred));
145+
let visited = &mut self.visited;
146+
let predicates = predicates.filter(|pred| visited.insert(pred));
147147

148148
self.stack.extend(predicates);
149149
}
@@ -298,13 +298,21 @@ impl<'tcx> TraitAliasExpansionInfo<'tcx> {
298298
}
299299
}
300300

301-
fn clone_and_push(&self, trait_ref: ty::PolyTraitRef<'tcx>, span: Span) -> Self {
302-
let mut path = self.path.clone();
303-
path.push((trait_ref, span));
304-
305-
Self {
306-
path
301+
/// Adds diagnostic labels to `diag` for the expansion path of a trait through all intermediate
302+
/// trait aliases.
303+
pub fn label_with_exp_info(&self,
304+
diag: &mut DiagnosticBuilder<'_>,
305+
top_label: &str,
306+
use_desc: &str
307+
) {
308+
diag.span_label(self.top().1, top_label);
309+
if self.path.len() > 1 {
310+
for (_, sp) in self.path.iter().rev().skip(1).take(self.path.len() - 2) {
311+
diag.span_label(*sp, format!("referenced here ({})", use_desc));
312+
}
307313
}
314+
diag.span_label(self.bottom().1,
315+
format!("trait alias used in trait object type ({})", use_desc));
308316
}
309317

310318
pub fn trait_ref(&self) -> &ty::PolyTraitRef<'tcx> {
@@ -318,33 +326,14 @@ impl<'tcx> TraitAliasExpansionInfo<'tcx> {
318326
pub fn bottom(&self) -> &(ty::PolyTraitRef<'tcx>, Span) {
319327
self.path.first().unwrap()
320328
}
321-
}
322329

323-
/// Emits diagnostic information relating to the expansion of a trait via trait aliases
324-
/// (see [`TraitAliasExpansionInfo`]).
325-
pub trait TraitAliasExpansionInfoDignosticBuilder {
326-
fn label_with_exp_info<'tcx>(&mut self,
327-
info: &TraitAliasExpansionInfo<'tcx>,
328-
top_label: &str,
329-
use_desc: &str
330-
) -> &mut Self;
331-
}
330+
fn clone_and_push(&self, trait_ref: ty::PolyTraitRef<'tcx>, span: Span) -> Self {
331+
let mut path = self.path.clone();
332+
path.push((trait_ref, span));
332333

333-
impl<'a> TraitAliasExpansionInfoDignosticBuilder for DiagnosticBuilder<'a> {
334-
fn label_with_exp_info<'tcx>(&mut self,
335-
info: &TraitAliasExpansionInfo<'tcx>,
336-
top_label: &str,
337-
use_desc: &str
338-
) -> &mut Self {
339-
self.span_label(info.top().1, top_label);
340-
if info.path.len() > 1 {
341-
for (_, sp) in info.path.iter().rev().skip(1).take(info.path.len() - 2) {
342-
self.span_label(*sp, format!("referenced here ({})", use_desc));
343-
}
334+
Self {
335+
path
344336
}
345-
self.span_label(info.bottom().1,
346-
format!("trait alias used in trait object type ({})", use_desc));
347-
self
348337
}
349338
}
350339

@@ -388,16 +377,15 @@ impl<'cx, 'gcx, 'tcx> TraitAliasExpander<'cx, 'gcx, 'tcx> {
388377
// Get components of trait alias.
389378
let predicates = tcx.super_predicates_of(trait_ref.def_id());
390379

391-
let items: Vec<_> = predicates.predicates
380+
let items = predicates.predicates
392381
.iter()
393382
.rev()
394383
.filter_map(|(pred, span)| {
395384
pred.subst_supertrait(tcx, &trait_ref)
396385
.to_opt_poly_trait_ref()
397386
.map(|trait_ref| item.clone_and_push(trait_ref, *span))
398-
})
399-
.collect();
400-
debug!("expand_trait_aliases: items={:?}", items);
387+
});
388+
debug!("expand_trait_aliases: items={:?}", items.clone());
401389

402390
self.stack.extend(items);
403391

src/librustc_typeck/astconv.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::lint;
1111
use crate::middle::resolve_lifetime as rl;
1212
use crate::namespace::Namespace;
1313
use rustc::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
14-
use rustc::traits::{self, TraitAliasExpansionInfoDignosticBuilder};
14+
use rustc::traits;
1515
use rustc::ty::{self, DefIdTree, Ty, TyCtxt, ToPredicate, TypeFoldable};
1616
use rustc::ty::{GenericParamDef, GenericParamDefKind};
1717
use rustc::ty::subst::{Kind, Subst, InternalSubsts, SubstsRef};
@@ -976,6 +976,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
976976
let mut projection_bounds = Vec::new();
977977
let mut potential_assoc_types = Vec::new();
978978
let dummy_self = self.tcx().types.trait_object_dummy_self;
979+
// FIXME: we want to avoid collecting into a `Vec` here, but simply cloning the iterator is
980+
// not straightforward due to the borrow checker.
979981
let bound_trait_refs: Vec<_> = trait_bounds
980982
.iter()
981983
.rev()
@@ -998,14 +1000,14 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
9981000
if regular_traits.len() > 1 {
9991001
let first_trait = &regular_traits[0];
10001002
let additional_trait = &regular_traits[1];
1001-
struct_span_err!(tcx.sess, additional_trait.bottom().1, E0225,
1003+
let mut err = struct_span_err!(tcx.sess, additional_trait.bottom().1, E0225,
10021004
"only auto traits can be used as additional traits in a trait object"
1003-
)
1004-
.label_with_exp_info(additional_trait, "additional non-auto trait",
1005-
"additional use")
1006-
.label_with_exp_info(first_trait, "first non-auto trait",
1007-
"first use")
1008-
.emit();
1005+
);
1006+
additional_trait.label_with_exp_info(&mut err,
1007+
"additional non-auto trait", "additional use");
1008+
first_trait.label_with_exp_info(&mut err,
1009+
"first non-auto trait", "first use");
1010+
err.emit();
10091011
}
10101012

10111013
if regular_traits.is_empty() && auto_traits.is_empty() {

0 commit comments

Comments
 (0)