Skip to content

Commit 71b31c4

Browse files
Merge bounds_span_for_suggestions_with_parentheses into bounds_span_for_suggestion
1 parent d00bf0a commit 71b31c4

File tree

5 files changed

+18
-31
lines changed

5 files changed

+18
-31
lines changed

compiler/rustc_hir/src/hir.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -644,24 +644,10 @@ impl<'hir> Generics<'hir> {
644644
})
645645
}
646646

647-
pub fn bounds_span_for_suggestions(&self, param_def_id: LocalDefId) -> Option<Span> {
648-
self.bounds_for_param(param_def_id).flat_map(|bp| bp.bounds.iter().rev()).find_map(
649-
|bound| {
650-
// We include bounds that come from a `#[derive(_)]` but point at the user's code,
651-
// as we use this method to get a span appropriate for suggestions.
652-
let bs = bound.span();
653-
bs.can_be_used_for_suggestions().then(|| bs.shrink_to_hi())
654-
},
655-
)
656-
}
657-
658647
/// Returns bounds span for suggestions.
659-
/// If the span including lifetime bound needs parentheses, it returns a tuple of a span to be surrounded by parentheses and true.
648+
/// If the span including lifetime bound needs parentheses, it returns a span to the open parenthese at the second item.
660649
/// e.g. `dyn Future<Output = ()> + 'static` needs parentheses `(dyn Future<Output = ()>) + 'static`
661-
pub fn bounds_span_for_suggestions_with_parentheses(
662-
&self,
663-
param_def_id: LocalDefId,
664-
) -> Option<(Span, bool)> {
650+
pub fn bounds_span_for_suggestions(&self, param_def_id: LocalDefId) -> Option<(Span, Option<Span>)> {
665651
fn get_inner_ty<'a, 'b>(bound: &'a GenericBound<'b>) -> Option<&'a Ty<'b>> {
666652
match bound {
667653
GenericBound::Trait(data, _) => {
@@ -695,10 +681,12 @@ impl<'hir> Generics<'hir> {
695681

696682
span_for_parentheses.map_or_else(
697683
|| {
684+
// We include bounds that come from a `#[derive(_)]` but point at the user's code,
685+
// as we use this method to get a span appropriate for suggestions.
698686
let bs = bound.span();
699-
bs.can_be_used_for_suggestions().then(|| (bs.shrink_to_hi(), false))
687+
bs.can_be_used_for_suggestions().then(|| (bs.shrink_to_hi(), None))
700688
},
701-
|span| Some((span, true)),
689+
|span| Some((span.shrink_to_hi(), Some(span.shrink_to_lo()))),
702690
)
703691
},
704692
)

compiler/rustc_hir_typeck/src/method/suggest.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3275,14 +3275,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32753275
param.name.ident(),
32763276
));
32773277
let bounds_span = hir_generics.bounds_span_for_suggestions(def_id);
3278-
if rcvr_ty.is_ref() && param.is_impl_trait() && bounds_span.is_some() {
3278+
if rcvr_ty.is_ref() && param.is_impl_trait() && let Some((bounds_span, _)) = bounds_span {
32793279
err.multipart_suggestions(
32803280
msg,
32813281
candidates.iter().map(|t| {
32823282
vec![
32833283
(param.span.shrink_to_lo(), "(".to_string()),
32843284
(
3285-
bounds_span.unwrap(),
3285+
bounds_span,
32863286
format!(" + {})", self.tcx.def_path_str(t.def_id)),
32873287
),
32883288
]
@@ -3292,7 +3292,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32923292
return;
32933293
}
32943294

3295-
let (sp, introducer) = if let Some(span) = bounds_span {
3295+
let (sp, introducer) = if let Some((span, _)) = bounds_span {
32963296
(span, Introducer::Plus)
32973297
} else if let Some(colon_span) = param.colon_span {
32983298
(colon_span.shrink_to_hi(), Introducer::Nothing)

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -2403,13 +2403,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
24032403
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
24042404
// instead we suggest `T: 'a + 'b` in that case.
24052405
let hir_generics = self.tcx.hir().get_generics(scope).unwrap();
2406-
let sugg_span = match hir_generics
2407-
.bounds_span_for_suggestions_with_parentheses(def_id)
2406+
let sugg_span = match hir_generics.bounds_span_for_suggestions(def_id)
24082407
{
2409-
Some((span, needs_parentheses)) => Some((span, true, needs_parentheses)),
2408+
Some((span, open_paren_sp)) => Some((span, true, open_paren_sp)),
24102409
// If `param` corresponds to `Self`, no usable suggestion span.
24112410
None if generics.has_self && param.index == 0 => None,
2412-
None => Some((self.tcx.def_span(def_id).shrink_to_hi(), false, false)),
2411+
None => Some((self.tcx.def_span(def_id).shrink_to_hi(), false, None)),
24132412
};
24142413
(scope, sugg_span)
24152414
}
@@ -2432,15 +2431,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
24322431
let mut suggs = vec![];
24332432
let lt_name = self.suggest_name_region(sub, &mut suggs);
24342433

2435-
if let Some((sp, has_lifetimes, needs_parentheses)) = type_param_sugg_span
2434+
if let Some((sp, has_lifetimes, open_paren_sp)) = type_param_sugg_span
24362435
&& suggestion_scope == type_scope
24372436
{
24382437
let suggestion =
24392438
if has_lifetimes { format!(" + {lt_name}") } else { format!(": {lt_name}") };
24402439

2441-
if needs_parentheses {
2442-
suggs.push((sp.shrink_to_lo(), "(".to_string()));
2443-
suggs.push((sp.shrink_to_hi(), format!("){suggestion}")));
2440+
if let Some(open_paren_sp) = open_paren_sp {
2441+
suggs.push((open_paren_sp, "(".to_string()));
2442+
suggs.push((sp, format!("){suggestion}")));
24442443
} else {
24452444
suggs.push((sp, suggestion))
24462445
}

compiler/rustc_middle/src/ty/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ pub fn suggest_constraining_type_params<'a>(
328328
// --
329329
// |
330330
// replace with: `T: Bar +`
331-
if let Some(span) = generics.bounds_span_for_suggestions(param.def_id) {
331+
if let Some((span, _)) = generics.bounds_span_for_suggestions(param.def_id) {
332332
suggest_restrict(span, true);
333333
continue;
334334
}

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2929,7 +2929,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
29292929
_ => {}
29302930
};
29312931
// Didn't add an indirection suggestion, so add a general suggestion to relax `Sized`.
2932-
let (span, separator) = if let Some(s) = generics.bounds_span_for_suggestions(param.def_id)
2932+
let (span, separator) = if let Some((s, _)) = generics.bounds_span_for_suggestions(param.def_id)
29332933
{
29342934
(s, " +")
29352935
} else {

0 commit comments

Comments
 (0)