Skip to content

Commit

Permalink
Rollup merge of rust-lang#121326 - fmease:detect-empty-leading-where-…
Browse files Browse the repository at this point in the history
…clauses-on-ty-aliases, r=compiler-errors

Detect empty leading where clauses on type aliases

1. commit: refactor the AST of type alias where clauses
   * I could no longer bear the look of `.0.1` and `.1.0`
   * Arguably moving `split` out of `TyAlias` into a substruct might not make that much sense from a semantic standpoint since it reprs an index into `TyAlias.predicates` but it's alright and it cleans up the usage sites of `TyAlias`
2. commit: fix an oversight: An empty leading where clause is still a leading where clause
   * semantically reject empty leading where clauses on lazy type aliases
     * e.g., on `#![feature(lazy_type_alias)] type X where = ();`
   * make empty leading where clauses on assoc types trigger lint `deprecated_where_clause_location`
     * e.g., `impl Trait for () { type X where = (); }`
  • Loading branch information
matthiaskrgr authored Feb 29, 2024
2 parents 6f2722b + fc64cbd commit 050610e
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,8 +1651,7 @@ struct TyAliasRewriteInfo<'c, 'g>(
&'c RewriteContext<'c>,
Indent,
&'g ast::Generics,
(ast::TyAliasWhereClause, ast::TyAliasWhereClause),
usize,
ast::TyAliasWhereClauses,
symbol::Ident,
Span,
);
Expand All @@ -1672,23 +1671,14 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
ref bounds,
ref ty,
where_clauses,
where_predicates_split,
} = *ty_alias_kind;
let ty_opt = ty.as_ref();
let (ident, vis) = match visitor_kind {
Item(i) => (i.ident, &i.vis),
AssocTraitItem(i) | AssocImplItem(i) => (i.ident, &i.vis),
ForeignItem(i) => (i.ident, &i.vis),
};
let rw_info = &TyAliasRewriteInfo(
context,
indent,
generics,
where_clauses,
where_predicates_split,
ident,
span,
);
let rw_info = &TyAliasRewriteInfo(context, indent, generics, where_clauses, ident, span);
let op_ty = opaque_ty(ty);
// Type Aliases are formatted slightly differently depending on the context
// in which they appear, whether they are opaque, and whether they are associated.
Expand Down Expand Up @@ -1724,19 +1714,11 @@ fn rewrite_ty<R: Rewrite>(
vis: &ast::Visibility,
) -> Option<String> {
let mut result = String::with_capacity(128);
let TyAliasRewriteInfo(
context,
indent,
generics,
where_clauses,
where_predicates_split,
ident,
span,
) = *rw_info;
let TyAliasRewriteInfo(context, indent, generics, where_clauses, ident, span) = *rw_info;
let (before_where_predicates, after_where_predicates) = generics
.where_clause
.predicates
.split_at(where_predicates_split);
.split_at(where_clauses.split);
if !after_where_predicates.is_empty() {
return None;
}
Expand Down Expand Up @@ -1771,7 +1753,7 @@ fn rewrite_ty<R: Rewrite>(
let where_clause_str = rewrite_where_clause(
context,
before_where_predicates,
where_clauses.0.1,
where_clauses.before.span,
context.config.brace_style(),
Shape::legacy(where_budget, indent),
false,
Expand All @@ -1795,7 +1777,7 @@ fn rewrite_ty<R: Rewrite>(
let comment_span = context
.snippet_provider
.opt_span_before(span, "=")
.map(|op_lo| mk_sp(where_clauses.0.1.hi(), op_lo));
.map(|op_lo| mk_sp(where_clauses.before.span.hi(), op_lo));

let lhs = match comment_span {
Some(comment_span)
Expand Down

0 comments on commit 050610e

Please sign in to comment.