Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hir::Generics: convert has_where_clause_predicates field into method #100113

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lifetime_res_to_generic_param(ident, node_id, res)
}));

let has_where_clause_predicates = !generics.where_clause.predicates.is_empty();
Copy link
Member

@compiler-errors compiler-errors Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking that the AST generics' predicates is empty is not the same as checking that the HIR generics' predicates is empty, since we process some bounds like Sized while lowering.

let where_clause_span = self.lower_span(generics.where_clause.span);
let span = self.lower_span(generics.span);
let res = f(self);
Expand All @@ -1390,7 +1389,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
let lowered_generics = self.arena.alloc(hir::Generics {
params: self.arena.alloc_from_iter(params),
predicates: self.arena.alloc_from_iter(predicates),
has_where_clause_predicates,
where_clause_span,
span,
});
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
generics: self.arena.alloc(hir::Generics {
params: lifetime_defs,
predicates: &[],
has_where_clause_predicates: false,
where_clause_span: lctx.lower_span(span),
span: lctx.lower_span(span),
}),
Expand Down Expand Up @@ -1717,7 +1716,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
generics: this.arena.alloc(hir::Generics {
params: generic_params,
predicates: &[],
has_where_clause_predicates: false,
where_clause_span: this.lower_span(span),
span: this.lower_span(span),
}),
Expand Down
20 changes: 9 additions & 11 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,20 +528,14 @@ pub struct GenericParamCount {
pub struct Generics<'hir> {
pub params: &'hir [GenericParam<'hir>],
pub predicates: &'hir [WherePredicate<'hir>],
pub has_where_clause_predicates: bool,
pub where_clause_span: Span,
pub span: Span,
}

impl<'hir> Generics<'hir> {
pub const fn empty() -> &'hir Generics<'hir> {
const NOPE: Generics<'_> = Generics {
params: &[],
predicates: &[],
has_where_clause_predicates: false,
where_clause_span: DUMMY_SP,
span: DUMMY_SP,
};
const NOPE: Generics<'_> =
Generics { params: &[], predicates: &[], where_clause_span: DUMMY_SP, span: DUMMY_SP };
&NOPE
}

Expand Down Expand Up @@ -578,7 +572,7 @@ impl<'hir> Generics<'hir> {
/// in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
pub fn tail_span_for_predicate_suggestion(&self) -> Span {
let end = self.where_clause_span.shrink_to_hi();
if self.has_where_clause_predicates {
if self.has_where_clause_predicates() {
self.predicates
.iter()
.filter(|p| p.in_where_clause())
Expand All @@ -592,7 +586,7 @@ impl<'hir> Generics<'hir> {
}

pub fn add_where_or_trailing_comma(&self) -> &'static str {
if self.has_where_clause_predicates {
if self.has_where_clause_predicates() {
","
} else if self.where_clause_span.is_empty() {
" where"
Expand Down Expand Up @@ -689,6 +683,10 @@ impl<'hir> Generics<'hir> {
bounds[bound_pos - 1].span().shrink_to_hi().to(span)
}
}

pub fn has_where_clause_predicates(&self) -> bool {
!self.where_clause_span.is_empty()
}
}

/// A single predicate in a where-clause.
Expand Down Expand Up @@ -3495,7 +3493,7 @@ mod size_asserts {
rustc_data_structures::static_assert_size!(Expr<'static>, 56);
rustc_data_structures::static_assert_size!(ForeignItem<'static>, 72);
rustc_data_structures::static_assert_size!(GenericBound<'_>, 48);
rustc_data_structures::static_assert_size!(Generics<'static>, 56);
rustc_data_structures::static_assert_size!(Generics<'static>, 48);
rustc_data_structures::static_assert_size!(ImplItem<'static>, 88);
rustc_data_structures::static_assert_size!(Impl<'static>, 80);
rustc_data_structures::static_assert_size!(Item<'static>, 80);
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2244,7 +2244,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {

// If all predicates are inferable, drop the entire clause
// (including the `where`)
if hir_generics.has_where_clause_predicates && dropped_predicate_count == num_predicates
if hir_generics.has_where_clause_predicates()
&& dropped_predicate_count == num_predicates
{
let where_span = hir_generics.where_clause_span;
// Extend the where clause back to the closing `>` of the
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub fn suggest_constraining_type_params<'a>(
continue;
}

if generics.has_where_clause_predicates {
if generics.has_where_clause_predicates() {
// This part is a bit tricky, because using the `where` clause user can
// provide zero, one or many bounds for the same type parameter, so we
// have following cases to consider:
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
.emit();
error = true;
}
if generics.has_where_clause_predicates {
if generics.has_where_clause_predicates() {
struct_span_err!(
tcx.sess,
generics.where_clause_span,
Expand Down