Skip to content

Commit 363024d

Browse files
estebankpietroalbini
authored andcommitted
Expand where negative supertrait specific error is shown
Fix rust-lang#58857.
1 parent ad293f1 commit 363024d

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ impl<'a> Parser<'a> {
17941794
let mut bounds = vec![GenericBound::Trait(poly_trait_ref, TraitBoundModifier::None)];
17951795
if parse_plus {
17961796
self.eat_plus(); // `+`, or `+=` gets split and `+` is discarded
1797-
bounds.append(&mut self.parse_generic_bounds(None)?);
1797+
bounds.append(&mut self.parse_generic_bounds(Some(self.prev_span))?);
17981798
}
17991799
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
18001800
}
@@ -5502,6 +5502,7 @@ impl<'a> Parser<'a> {
55025502
let mut bounds = Vec::new();
55035503
let mut negative_bounds = Vec::new();
55045504
let mut last_plus_span = None;
5505+
let mut was_negative = false;
55055506
loop {
55065507
// This needs to be synchronized with `Token::can_begin_bound`.
55075508
let is_bound_start = self.check_path() || self.check_lifetime() ||
@@ -5546,9 +5547,10 @@ impl<'a> Parser<'a> {
55465547
}
55475548
let poly_span = lo.to(self.prev_span);
55485549
if is_negative {
5549-
negative_bounds.push(
5550-
last_plus_span.or(colon_span).unwrap()
5551-
.to(poly_span));
5550+
was_negative = true;
5551+
if let Some(sp) = last_plus_span.or(colon_span) {
5552+
negative_bounds.push(sp.to(poly_span));
5553+
}
55525554
} else {
55535555
let poly_trait = PolyTraitRef::new(lifetime_defs, path, poly_span);
55545556
let modifier = if question.is_some() {
@@ -5570,26 +5572,28 @@ impl<'a> Parser<'a> {
55705572
}
55715573
}
55725574

5573-
if !negative_bounds.is_empty() {
5575+
if !negative_bounds.is_empty() || was_negative {
55745576
let plural = negative_bounds.len() > 1;
55755577
let mut err = self.struct_span_err(negative_bounds,
55765578
"negative trait bounds are not supported");
5577-
let bound_list = colon_span.unwrap().to(self.prev_span);
5578-
let mut new_bound_list = String::new();
5579-
if !bounds.is_empty() {
5580-
let mut snippets = bounds.iter().map(|bound| bound.span())
5581-
.map(|span| self.sess.source_map().span_to_snippet(span));
5582-
while let Some(Ok(snippet)) = snippets.next() {
5583-
new_bound_list.push_str(" + ");
5584-
new_bound_list.push_str(&snippet);
5585-
}
5586-
new_bound_list = new_bound_list.replacen(" +", ":", 1);
5587-
}
5588-
err.span_suggestion_short(bound_list,
5589-
&format!("remove the trait bound{}",
5590-
if plural { "s" } else { "" }),
5591-
new_bound_list,
5592-
Applicability::MachineApplicable);
5579+
if let Some(bound_list) = colon_span {
5580+
let bound_list = bound_list.to(self.prev_span);
5581+
let mut new_bound_list = String::new();
5582+
if !bounds.is_empty() {
5583+
let mut snippets = bounds.iter().map(|bound| bound.span())
5584+
.map(|span| self.sess.source_map().span_to_snippet(span));
5585+
while let Some(Ok(snippet)) = snippets.next() {
5586+
new_bound_list.push_str(" + ");
5587+
new_bound_list.push_str(&snippet);
5588+
}
5589+
new_bound_list = new_bound_list.replacen(" +", ":", 1);
5590+
}
5591+
err.span_suggestion_short(bound_list,
5592+
&format!("remove the trait bound{}",
5593+
if plural { "s" } else { "" }),
5594+
new_bound_list,
5595+
Applicability::MachineApplicable);
5596+
}
55935597
err.emit();
55945598
}
55955599

@@ -5625,7 +5629,7 @@ impl<'a> Parser<'a> {
56255629

56265630
// Parse optional colon and param bounds.
56275631
let bounds = if self.eat(&token::Colon) {
5628-
self.parse_generic_bounds(None)?
5632+
self.parse_generic_bounds(Some(self.prev_span))?
56295633
} else {
56305634
Vec::new()
56315635
};
@@ -6070,7 +6074,7 @@ impl<'a> Parser<'a> {
60706074
// or with mandatory equality sign and the second type.
60716075
let ty = self.parse_ty()?;
60726076
if self.eat(&token::Colon) {
6073-
let bounds = self.parse_generic_bounds(None)?;
6077+
let bounds = self.parse_generic_bounds(Some(self.prev_span))?;
60746078
where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
60756079
ast::WhereBoundPredicate {
60766080
span: lo.to(self.prev_span),
@@ -7626,7 +7630,7 @@ impl<'a> Parser<'a> {
76267630
tps.where_clause = self.parse_where_clause()?;
76277631
let alias = if existential {
76287632
self.expect(&token::Colon)?;
7629-
let bounds = self.parse_generic_bounds(None)?;
7633+
let bounds = self.parse_generic_bounds(Some(self.prev_span))?;
76307634
AliasKind::Existential(bounds)
76317635
} else {
76327636
self.expect(&token::Eq)?;

src/test/ui/issues/issue-58857.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct Conj<A> {a : A}
2+
trait Valid {}
3+
4+
impl<A: !Valid> Conj<A>{}
5+
//~^ ERROR negative trait bounds are not supported
6+
7+
fn main() {}

src/test/ui/issues/issue-58857.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: negative trait bounds are not supported
2+
--> $DIR/issue-58857.rs:4:7
3+
|
4+
LL | impl<A: !Valid> Conj<A>{}
5+
| ^^^^^^^^ help: remove the trait bound
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)