Skip to content

Commit b714f5c

Browse files
authored
Rollup merge of #72715 - estebank:trailing-comma-where, r=petrochenkov
Account for trailing comma when suggesting `where` clauses Fix #72693.
2 parents 3bbb475 + 0d18136 commit b714f5c

File tree

4 files changed

+10
-17
lines changed

4 files changed

+10
-17
lines changed

src/librustc_hir/hir.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,13 @@ impl WhereClause<'_> {
524524
pub fn span_for_predicates_or_empty_place(&self) -> Span {
525525
self.span
526526
}
527+
528+
/// `Span` where further predicates would be suggested, accounting for trailing commas, like
529+
/// in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
530+
pub fn tail_span_for_suggestion(&self) -> Span {
531+
let end = self.span_for_predicates_or_empty_place().shrink_to_hi();
532+
self.predicates.last().map(|p| p.span()).unwrap_or(end).shrink_to_hi().to(end)
533+
}
527534
}
528535

529536
/// A single predicate in a where-clause.

src/librustc_middle/ty/diagnostics.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
77
use rustc_hir as hir;
88
use rustc_hir::def_id::DefId;
99
use rustc_hir::{QPath, TyKind, WhereBoundPredicate, WherePredicate};
10-
use rustc_span::{BytePos, Span};
1110

1211
impl<'tcx> TyS<'tcx> {
1312
/// Similar to `TyS::is_primitive`, but also considers inferred numeric values to be primitive.
@@ -221,24 +220,11 @@ pub fn suggest_constraining_type_param(
221220
}
222221
}
223222

224-
let where_clause_span = generics.where_clause.span_for_predicates_or_empty_place();
225-
// Account for `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
226-
let mut trailing_comma = false;
227-
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(where_clause_span) {
228-
trailing_comma = snippet.ends_with(',');
229-
}
230-
let where_clause_span = if trailing_comma {
231-
let hi = where_clause_span.hi();
232-
Span::new(hi - BytePos(1), hi, where_clause_span.ctxt())
233-
} else {
234-
where_clause_span.shrink_to_hi()
235-
};
236-
237223
match &param_spans[..] {
238224
&[&param_span] => suggest_restrict(param_span.shrink_to_hi()),
239225
_ => {
240226
err.span_suggestion_verbose(
241-
where_clause_span,
227+
generics.where_clause.tail_span_for_suggestion(),
242228
&msg_restrict_type_further,
243229
format!(", {}: {}", param_name, constraint),
244230
Applicability::MachineApplicable,

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub trait InferCtxtExt<'tcx> {
170170

171171
fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
172172
(
173-
generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi(),
173+
generics.where_clause.tail_span_for_suggestion(),
174174
format!(
175175
"{} {}",
176176
if !generics.where_clause.predicates.is_empty() { "," } else { " where" },

src/test/ui/bound-suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn test_one_bound<T: Sized>(t: T) {
1919
}
2020

2121
#[allow(dead_code)]
22-
fn test_no_bounds_where<X, Y>(x: X, y: Y) where X: std::fmt::Debug {
22+
fn test_no_bounds_where<X, Y>(x: X, y: Y) where X: std::fmt::Debug, {
2323
println!("{:?} {:?}", x, y);
2424
//~^ ERROR doesn't implement
2525
}

0 commit comments

Comments
 (0)