Skip to content

Commit 91b9988

Browse files
Address comments
1 parent d69aed1 commit 91b9988

File tree

8 files changed

+131
-85
lines changed

8 files changed

+131
-85
lines changed

compiler/rustc_ast_lowering/src/item.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
13771377
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> =
13781378
self.lower_generic_params_mut(&generics.params).collect();
13791379
let has_where_clause_predicates = !generics.where_clause.predicates.is_empty();
1380-
let has_where_clause_token = generics.where_clause.has_where_token;
13811380
let where_clause_span = self.lower_span(generics.where_clause.span);
13821381
let span = self.lower_span(generics.span);
13831382
let res = f(self);
@@ -1396,7 +1395,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
13961395
params: self.arena.alloc_from_iter(params),
13971396
predicates: self.arena.alloc_from_iter(predicates),
13981397
has_where_clause_predicates,
1399-
has_where_clause_token,
14001398
where_clause_span,
14011399
span,
14021400
});

compiler/rustc_ast_lowering/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13331333
params: lifetime_defs,
13341334
predicates: &[],
13351335
has_where_clause_predicates: false,
1336-
has_where_clause_token: false,
13371336
where_clause_span: lctx.lower_span(span),
13381337
span: lctx.lower_span(span),
13391338
}),
@@ -1656,7 +1655,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16561655
params: generic_params,
16571656
predicates: &[],
16581657
has_where_clause_predicates: false,
1659-
has_where_clause_token: false,
16601658
where_clause_span: this.lower_span(span),
16611659
span: this.lower_span(span),
16621660
}),

compiler/rustc_hir/src/hir.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,6 @@ pub struct Generics<'hir> {
536536
pub params: &'hir [GenericParam<'hir>],
537537
pub predicates: &'hir [WherePredicate<'hir>],
538538
pub has_where_clause_predicates: bool,
539-
pub has_where_clause_token: bool,
540539
pub where_clause_span: Span,
541540
pub span: Span,
542541
}
@@ -547,7 +546,6 @@ impl<'hir> Generics<'hir> {
547546
params: &[],
548547
predicates: &[],
549548
has_where_clause_predicates: false,
550-
has_where_clause_token: false,
551549
where_clause_span: DUMMY_SP,
552550
span: DUMMY_SP,
553551
};
@@ -583,10 +581,6 @@ impl<'hir> Generics<'hir> {
583581
}
584582
}
585583

586-
pub fn where_clause_span(&self) -> Option<Span> {
587-
if self.predicates.is_empty() { None } else { Some(self.where_clause_span) }
588-
}
589-
590584
/// `Span` where further predicates would be suggested, accounting for trailing commas, like
591585
/// in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
592586
pub fn tail_span_for_predicate_suggestion(&self) -> Span {
@@ -607,10 +601,11 @@ impl<'hir> Generics<'hir> {
607601
pub fn add_where_or_trailing_comma(&self) -> &'static str {
608602
if self.has_where_clause_predicates {
609603
","
610-
} else if self.has_where_clause_token {
611-
""
612-
} else {
604+
} else if self.where_clause_span.is_empty() {
613605
" where"
606+
} else {
607+
// No where clause predicates, but we have `where` token
608+
""
614609
}
615610
}
616611

compiler/rustc_lint/src/builtin.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2266,9 +2266,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22662266
// (including the `where`)
22672267
if hir_generics.has_where_clause_predicates && dropped_predicate_count == num_predicates
22682268
{
2269-
let where_span = hir_generics
2270-
.where_clause_span()
2271-
.expect("span of (nonempty) where clause should exist");
2269+
let where_span = hir_generics.where_clause_span;
22722270
// Extend the where clause back to the closing `>` of the
22732271
// generics, except for tuple struct, which have the `where`
22742272
// after the fields of the struct.

compiler/rustc_middle/src/ty/diagnostics.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,21 @@ impl<'tcx> Ty<'tcx> {
7676
}
7777

7878
pub trait IsSuggestable<'tcx> {
79+
/// Whether this makes sense to suggest in a diagnostic.
80+
///
81+
/// We filter out certain types and constants since they don't provide
82+
/// meaningful rendered suggestions when pretty-printed. We leave some
83+
/// nonsense, such as region vars, since those render as `'_` and are
84+
/// usually okay to reinterpret as elided lifetimes.
7985
fn is_suggestable(self, tcx: TyCtxt<'tcx>) -> bool;
80-
81-
fn is_suggestable_modulo_impl_trait(self, tcx: TyCtxt<'tcx>, bound_str: &str) -> bool;
8286
}
8387

8488
impl<'tcx, T> IsSuggestable<'tcx> for T
8589
where
8690
T: TypeFoldable<'tcx>,
8791
{
8892
fn is_suggestable(self, tcx: TyCtxt<'tcx>) -> bool {
89-
self.visit_with(&mut IsSuggestableVisitor { tcx, bound_str: None }).is_continue()
90-
}
91-
92-
fn is_suggestable_modulo_impl_trait(self, tcx: TyCtxt<'tcx>, bound_str: &str) -> bool {
93-
self.visit_with(&mut IsSuggestableVisitor { tcx, bound_str: Some(bound_str) }).is_continue()
93+
self.visit_with(&mut IsSuggestableVisitor { tcx }).is_continue()
9494
}
9595
}
9696

@@ -119,7 +119,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
119119
&format!(
120120
"consider {} `where` clause, but there might be an alternative better way to express \
121121
this requirement",
122-
if generics.has_where_clause_token { "extending the" } else { "introducing a" },
122+
if generics.where_clause_span.is_empty() { "introducing a" } else { "extending the" },
123123
),
124124
format!("{} {}: {}", generics.add_where_or_trailing_comma(), param_name, constraint),
125125
Applicability::MaybeIncorrect,
@@ -417,12 +417,11 @@ impl<'v> hir::intravisit::Visitor<'v> for StaticLifetimeVisitor<'v> {
417417
}
418418
}
419419

420-
pub struct IsSuggestableVisitor<'tcx, 's> {
420+
pub struct IsSuggestableVisitor<'tcx> {
421421
tcx: TyCtxt<'tcx>,
422-
bound_str: Option<&'s str>,
423422
}
424423

425-
impl<'tcx> TypeVisitor<'tcx> for IsSuggestableVisitor<'tcx, '_> {
424+
impl<'tcx> TypeVisitor<'tcx> for IsSuggestableVisitor<'tcx> {
426425
type BreakTy = ();
427426

428427
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
@@ -462,12 +461,13 @@ impl<'tcx> TypeVisitor<'tcx> for IsSuggestableVisitor<'tcx, '_> {
462461
}
463462

464463
Param(param) => {
465-
if let Some(found_bound_str) =
466-
param.name.as_str().strip_prefix("impl ").map(|s| s.trim_start())
467-
{
468-
if self.bound_str.map_or(true, |bound_str| bound_str != found_bound_str) {
469-
return ControlFlow::Break(());
470-
}
464+
// FIXME: It would be nice to make this not use string manipulation,
465+
// but it's pretty hard to do this, since `ty::ParamTy` is missing
466+
// sufficient info to determine if it is synthetic, and we don't
467+
// always have a convenient way of getting `ty::Generics` at the call
468+
// sites we invoke `IsSuggestable::is_suggestable`.
469+
if param.name.as_str().starts_with("impl ") {
470+
return ControlFlow::Break(());
471471
}
472472
}
473473

compiler/rustc_middle/src/ty/generics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ impl GenericParamDefKind {
3939
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true,
4040
}
4141
}
42+
43+
pub fn is_synthetic(&self) -> bool {
44+
match self {
45+
GenericParamDefKind::Type { synthetic, .. } => *synthetic,
46+
_ => false,
47+
}
48+
}
4249
}
4350

4451
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]

0 commit comments

Comments
 (0)