mirror of
https://github.com/rust-lang/rust
synced 2024-10-14 12:33:57 +00:00
Rollup merge of #72715 - estebank:trailing-comma-where, r=petrochenkov
Account for trailing comma when suggesting `where` clauses Fix #72693.
This commit is contained in:
commit
b714f5c9ac
|
@ -524,6 +524,13 @@ pub fn span(&self) -> Option<Span> {
|
|||
pub fn span_for_predicates_or_empty_place(&self) -> Span {
|
||||
self.span
|
||||
}
|
||||
|
||||
/// `Span` where further predicates would be suggested, accounting for trailing commas, like
|
||||
/// in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
|
||||
pub fn tail_span_for_suggestion(&self) -> Span {
|
||||
let end = self.span_for_predicates_or_empty_place().shrink_to_hi();
|
||||
self.predicates.last().map(|p| p.span()).unwrap_or(end).shrink_to_hi().to(end)
|
||||
}
|
||||
}
|
||||
|
||||
/// A single predicate in a where-clause.
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::{QPath, TyKind, WhereBoundPredicate, WherePredicate};
|
||||
use rustc_span::{BytePos, Span};
|
||||
|
||||
impl<'tcx> TyS<'tcx> {
|
||||
/// Similar to `TyS::is_primitive`, but also considers inferred numeric values to be primitive.
|
||||
|
@ -221,24 +220,11 @@ pub fn suggest_constraining_type_param(
|
|||
}
|
||||
}
|
||||
|
||||
let where_clause_span = generics.where_clause.span_for_predicates_or_empty_place();
|
||||
// Account for `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
|
||||
let mut trailing_comma = false;
|
||||
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(where_clause_span) {
|
||||
trailing_comma = snippet.ends_with(',');
|
||||
}
|
||||
let where_clause_span = if trailing_comma {
|
||||
let hi = where_clause_span.hi();
|
||||
Span::new(hi - BytePos(1), hi, where_clause_span.ctxt())
|
||||
} else {
|
||||
where_clause_span.shrink_to_hi()
|
||||
};
|
||||
|
||||
match ¶m_spans[..] {
|
||||
&[¶m_span] => suggest_restrict(param_span.shrink_to_hi()),
|
||||
_ => {
|
||||
err.span_suggestion_verbose(
|
||||
where_clause_span,
|
||||
generics.where_clause.tail_span_for_suggestion(),
|
||||
&msg_restrict_type_further,
|
||||
format!(", {}: {}", param_name, constraint),
|
||||
Applicability::MachineApplicable,
|
||||
|
|
|
@ -170,7 +170,7 @@ fn suggest_await_before_try(
|
|||
|
||||
fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
|
||||
(
|
||||
generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi(),
|
||||
generics.where_clause.tail_span_for_suggestion(),
|
||||
format!(
|
||||
"{} {}",
|
||||
if !generics.where_clause.predicates.is_empty() { "," } else { " where" },
|
||||
|
|
|
@ -19,7 +19,7 @@ fn test_one_bound<T: Sized>(t: T) {
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn test_no_bounds_where<X, Y>(x: X, y: Y) where X: std::fmt::Debug {
|
||||
fn test_no_bounds_where<X, Y>(x: X, y: Y) where X: std::fmt::Debug, {
|
||||
println!("{:?} {:?}", x, y);
|
||||
//~^ ERROR doesn't implement
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue