Skip to content

Commit 0d18136

Browse files
committed
Move common code to WhereClause
1 parent 0424371 commit 0d18136

File tree

3 files changed

+9
-22
lines changed

3 files changed

+9
-22
lines changed

src/librustc_hir/hir.rs

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

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

src/librustc_middle/ty/diagnostics.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,11 @@ pub fn suggest_constraining_type_param(
220220
}
221221
}
222222

223-
// Account for `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
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);
233-
234223
match &param_spans[..] {
235224
&[&param_span] => suggest_restrict(param_span.shrink_to_hi()),
236225
_ => {
237226
err.span_suggestion_verbose(
238-
where_clause_span,
227+
generics.where_clause.tail_span_for_suggestion(),
239228
&msg_restrict_type_further,
240229
format!(", {}: {}", param_name, constraint),
241230
Applicability::MachineApplicable,

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,8 @@ 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();
173172
(
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),
173+
generics.where_clause.tail_span_for_suggestion(),
183174
format!(
184175
"{} {}",
185176
if !generics.where_clause.predicates.is_empty() { "," } else { " where" },

0 commit comments

Comments
 (0)