Skip to content

Commit 7f4a1a4

Browse files
committed
Deny parenthetical notation for negative bounds
1 parent e4c626d commit 7f4a1a4

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ ast_passes_module_nonascii = trying to load file for module `{$name}` with non-a
186186
ast_passes_negative_bound_not_supported =
187187
negative bounds are not supported
188188
189+
ast_passes_negative_bound_with_parenthetical_notation =
190+
parenthetical notation may not be used for negative bounds
191+
189192
ast_passes_nested_impl_trait = nested `impl Trait` is not allowed
190193
.outer = outer `impl Trait`
191194
.inner = nested `impl Trait` here

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,13 +1254,24 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12541254
if let GenericBound::Trait(trait_ref, modifiers) = bound
12551255
&& let BoundPolarity::Negative(_) = modifiers.polarity
12561256
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
1257-
&& let Some(ast::GenericArgs::AngleBracketed(args)) = segment.args.as_deref()
12581257
{
1259-
for arg in &args.args {
1260-
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
1261-
self.dcx()
1262-
.emit_err(errors::ConstraintOnNegativeBound { span: constraint.span });
1258+
match segment.args.as_deref() {
1259+
Some(ast::GenericArgs::AngleBracketed(args)) => {
1260+
for arg in &args.args {
1261+
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
1262+
self.dcx().emit_err(errors::ConstraintOnNegativeBound {
1263+
span: constraint.span,
1264+
});
1265+
}
1266+
}
1267+
}
1268+
// The lowered form of parenthesized generic args contains a type binding.
1269+
Some(ast::GenericArgs::Parenthesized(args)) => {
1270+
self.dcx().emit_err(errors::NegativeBoundWithParentheticalNotation {
1271+
span: args.span,
1272+
});
12631273
}
1274+
None => {}
12641275
}
12651276
}
12661277

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,13 @@ pub struct ConstraintOnNegativeBound {
738738
pub span: Span,
739739
}
740740

741+
#[derive(Diagnostic)]
742+
#[diag(ast_passes_negative_bound_with_parenthetical_notation)]
743+
pub struct NegativeBoundWithParentheticalNotation {
744+
#[primary_span]
745+
pub span: Span,
746+
}
747+
741748
#[derive(Diagnostic)]
742749
#[diag(ast_passes_invalid_unnamed_field_ty)]
743750
pub struct InvalidUnnamedFieldTy {

tests/ui/traits/negative-bounds/associated-constraints.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ fn test3<T: !Trait<Assoc: Send>>() {}
1717
fn test4<T>() where T: !Trait<Assoc: Send> {}
1818
//~^ ERROR associated type constraints not allowed on negative bounds
1919

20+
fn test5<T>() where T: !Fn() -> i32 {}
21+
//~^ ERROR parenthetical notation may not be used for negative bounds
22+
2023
fn main() {}

tests/ui/traits/negative-bounds/associated-constraints.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ error: associated type constraints not allowed on negative bounds
2222
LL | fn test4<T>() where T: !Trait<Assoc: Send> {}
2323
| ^^^^^^^^^^^
2424

25+
error: parenthetical notation may not be used for negative bounds
26+
--> $DIR/associated-constraints.rs:20:25
27+
|
28+
LL | fn test5<T>() where T: !Fn() -> i32 {}
29+
| ^^^^^^^^^^^
30+
2531
warning: the feature `negative_bounds` is incomplete and may not be safe to use and/or cause compiler crashes
2632
--> $DIR/associated-constraints.rs:1:12
2733
|
@@ -30,5 +36,5 @@ LL | #![feature(negative_bounds, associated_type_bounds)]
3036
|
3137
= note: `#[warn(incomplete_features)]` on by default
3238

33-
error: aborting due to 4 previous errors; 1 warning emitted
39+
error: aborting due to 5 previous errors; 1 warning emitted
3440

0 commit comments

Comments
 (0)