Skip to content

Commit 0424371

Browse files
committed
Account for trailing comma when suggesting where clauses
Fix #72693.
1 parent ff991d6 commit 0424371

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/librustc_middle/ty/diagnostics.rs

Lines changed: 9 additions & 12 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,18 +220,16 @@ pub fn suggest_constraining_type_param(
221220
}
222221
}
223222

224-
let where_clause_span = generics.where_clause.span_for_predicates_or_empty_place();
225223
// 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-
};
224+
let end = generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi();
225+
let where_clause_span = generics
226+
.where_clause
227+
.predicates
228+
.last()
229+
.map(|p| p.span())
230+
.unwrap_or(end)
231+
.shrink_to_hi()
232+
.to(end);
236233

237234
match &param_spans[..] {
238235
&[&param_span] => suggest_restrict(param_span.shrink_to_hi()),

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,17 @@ pub trait InferCtxtExt<'tcx> {
169169
}
170170

171171
fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
172+
let end = generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi();
172173
(
173-
generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi(),
174+
// Account for `where T: Foo,` so we don't suggest two trailing commas.
175+
generics
176+
.where_clause
177+
.predicates
178+
.last()
179+
.map(|p| p.span())
180+
.unwrap_or(end)
181+
.shrink_to_hi()
182+
.to(end),
174183
format!(
175184
"{} {}",
176185
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)