Skip to content

Commit a78592e

Browse files
committed
Fix split calculation in type alias where clauses
1 parent ac951d3 commit a78592e

File tree

8 files changed

+115
-6
lines changed

8 files changed

+115
-6
lines changed

compiler/rustc_ast/src/ast.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3402,7 +3402,9 @@ pub struct TyAliasWhereClauses {
34023402
/// The index in `TyAlias.generics.where_clause.predicates` that would split
34033403
/// into predicates from the where clause before the equals sign and the ones
34043404
/// from the where clause after the equals sign.
3405-
pub split: usize,
3405+
pub before_count: usize,
3406+
/// The count of the clauses that appear before the equals sign and have attrbutes.
3407+
pub before_with_attr_count: usize,
34063408
}
34073409

34083410
#[derive(Clone, Encodable, Decodable, Debug)]

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ fn walk_generics<T: MutVisitor>(vis: &mut T, generics: &mut Generics) {
10911091
}
10921092

10931093
fn walk_ty_alias_where_clauses<T: MutVisitor>(vis: &mut T, tawcs: &mut TyAliasWhereClauses) {
1094-
let TyAliasWhereClauses { before, after, split: _ } = tawcs;
1094+
let TyAliasWhereClauses { before, after, before_count: _, before_with_attr_count: _ } = tawcs;
10951095
let TyAliasWhereClause { has_where_token: _, span: span_before } = before;
10961096
let TyAliasWhereClause { has_where_token: _, span: span_after } = after;
10971097
vis.visit_span(span_before);

compiler/rustc_ast_passes/src/ast_validation.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ impl<'a> AstValidator<'a> {
136136
return Ok(());
137137
}
138138

139+
let split =
140+
ty_alias.where_clauses.before_count - ty_alias.where_clauses.before_with_attr_count;
139141
let (before_predicates, after_predicates) =
140-
ty_alias.generics.where_clause.predicates.split_at(ty_alias.where_clauses.split);
142+
ty_alias.generics.where_clause.predicates.split_at(split);
141143
let span = ty_alias.where_clauses.before.span;
142144

143145
let sugg = if !before_predicates.is_empty() || !ty_alias.where_clauses.after.has_where_token

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'a> State<'a> {
125125
defaultness: ast::Defaultness,
126126
) {
127127
let (before_predicates, after_predicates) =
128-
generics.where_clause.predicates.split_at(where_clauses.split);
128+
generics.where_clause.predicates.split_at(where_clauses.before_count);
129129
self.head("");
130130
self.print_visibility(vis);
131131
self.print_defaultness(defaultness);

compiler/rustc_parse/src/parser/item.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,12 @@ impl<'a> Parser<'a> {
10241024
has_where_token: after_where_clause.has_where_token,
10251025
span: after_where_clause.span,
10261026
},
1027-
split: before_where_clause.predicates.len(),
1027+
before_count: before_where_clause.predicates.len(),
1028+
before_with_attr_count: before_where_clause
1029+
.predicates
1030+
.iter()
1031+
.filter(|p| !p.attrs.is_empty())
1032+
.count(),
10281033
};
10291034
let mut predicates = before_where_clause.predicates;
10301035
predicates.extend(after_where_clause.predicates);

src/tools/rustfmt/src/items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,7 @@ fn rewrite_ty<R: Rewrite>(
17681768
let (before_where_predicates, after_where_predicates) = generics
17691769
.where_clause
17701770
.predicates
1771-
.split_at(where_clauses.split);
1771+
.split_at(where_clauses.before_count);
17721772
result.push_str(&format!("{}type ", format_visibility(context, vis)));
17731773
let ident_str = rewrite_ident(context, ident);
17741774

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![feature(lazy_type_alias)]
2+
//~^ WARNING: the feature `lazy_type_alias` is incomplete and may not be safe to use and/or cause compiler crashes [incomplete_features]
3+
4+
trait TraitA {}
5+
trait TraitB {}
6+
7+
fn this(_x: &()) {}
8+
9+
fn needs_copy<T>() {
10+
type F<T>
11+
where
12+
//~^ ERROR: where clauses are not allowed before the type for type aliases
13+
#[cfg(a)]
14+
//~^ ERROR: attributes in `where` clause are unstable
15+
//~| WARNING: unexpected `cfg` condition name: `a`
16+
T: TraitA,
17+
#[cfg(b)]
18+
//~^ ERROR: attributes in `where` clause are unstable
19+
//~| WARNING: unexpected `cfg` condition name: `b`
20+
T: TraitB,
21+
():,
22+
():,
23+
= T;
24+
let x: () = ();
25+
this(&x);
26+
}
27+
28+
fn main() {
29+
needs_copy::<()>();
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
error: where clauses are not allowed before the type for type aliases
2+
--> $DIR/cfg-attr-issue-138010.rs:11:5
3+
|
4+
LL | / where
5+
LL | |
6+
LL | | #[cfg(a)]
7+
... |
8+
LL | | ():,
9+
LL | | ():,
10+
| |____________^
11+
|
12+
= note: see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information
13+
help: move it to the end of the type declaration
14+
|
15+
LL ~
16+
LL ~ = T where ():, ():;
17+
|
18+
19+
error[E0658]: attributes in `where` clause are unstable
20+
--> $DIR/cfg-attr-issue-138010.rs:13:9
21+
|
22+
LL | #[cfg(a)]
23+
| ^^^^^^^^^
24+
|
25+
= note: see issue #115590 <https://github.com/rust-lang/rust/issues/115590> for more information
26+
= help: add `#![feature(where_clause_attrs)]` to the crate attributes to enable
27+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
28+
29+
error[E0658]: attributes in `where` clause are unstable
30+
--> $DIR/cfg-attr-issue-138010.rs:17:9
31+
|
32+
LL | #[cfg(b)]
33+
| ^^^^^^^^^
34+
|
35+
= note: see issue #115590 <https://github.com/rust-lang/rust/issues/115590> for more information
36+
= help: add `#![feature(where_clause_attrs)]` to the crate attributes to enable
37+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
38+
39+
warning: the feature `lazy_type_alias` is incomplete and may not be safe to use and/or cause compiler crashes
40+
--> $DIR/cfg-attr-issue-138010.rs:1:12
41+
|
42+
LL | #![feature(lazy_type_alias)]
43+
| ^^^^^^^^^^^^^^^
44+
|
45+
= note: see issue #112792 <https://github.com/rust-lang/rust/issues/112792> for more information
46+
= note: `#[warn(incomplete_features)]` on by default
47+
48+
warning: unexpected `cfg` condition name: `a`
49+
--> $DIR/cfg-attr-issue-138010.rs:13:15
50+
|
51+
LL | #[cfg(a)]
52+
| ^ help: found config with similar value: `target_feature = "a"`
53+
|
54+
= help: expected names are: `FALSE` and `test` and 31 more
55+
= help: to expect this configuration use `--check-cfg=cfg(a)`
56+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
57+
= note: `#[warn(unexpected_cfgs)]` on by default
58+
59+
warning: unexpected `cfg` condition name: `b`
60+
--> $DIR/cfg-attr-issue-138010.rs:17:15
61+
|
62+
LL | #[cfg(b)]
63+
| ^
64+
|
65+
= help: to expect this configuration use `--check-cfg=cfg(b)`
66+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
67+
68+
error: aborting due to 3 previous errors; 3 warnings emitted
69+
70+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)