Skip to content

Commit a251974

Browse files
committed
Deny parenthetical notation for negative bounds
1 parent 5f56465 commit a251974

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

compiler/rustc_ast_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ ast_passes_module_nonascii = trying to load file for module `{$name}` with non-a
188188
ast_passes_negative_bound_not_supported =
189189
negative bounds are not supported
190190
191+
ast_passes_negative_bound_with_parenthetical_notation =
192+
parenthetical notation may not be used for negative bounds
193+
191194
ast_passes_nested_impl_trait = nested `impl Trait` is not allowed
192195
.outer = outer `impl Trait`
193196
.inner = nested `impl Trait` here

compiler/rustc_ast_passes/src/ast_validation.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1257,13 +1257,24 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12571257
if let GenericBound::Trait(trait_ref, modifiers) = bound
12581258
&& let BoundPolarity::Negative(_) = modifiers.polarity
12591259
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
1260-
&& let Some(ast::GenericArgs::AngleBracketed(args)) = segment.args.as_deref()
12611260
{
1262-
for arg in &args.args {
1263-
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
1264-
self.dcx()
1265-
.emit_err(errors::ConstraintOnNegativeBound { span: constraint.span });
1261+
match segment.args.as_deref() {
1262+
Some(ast::GenericArgs::AngleBracketed(args)) => {
1263+
for arg in &args.args {
1264+
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
1265+
self.dcx().emit_err(errors::ConstraintOnNegativeBound {
1266+
span: constraint.span,
1267+
});
1268+
}
1269+
}
1270+
}
1271+
// The lowered form of parenthesized generic args contains a type binding.
1272+
Some(ast::GenericArgs::Parenthesized(args)) => {
1273+
self.dcx().emit_err(errors::NegativeBoundWithParentheticalNotation {
1274+
span: args.span,
1275+
});
12661276
}
1277+
None => {}
12671278
}
12681279
}
12691280

compiler/rustc_ast_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,13 @@ pub struct ConstraintOnNegativeBound {
745745
pub span: Span,
746746
}
747747

748+
#[derive(Diagnostic)]
749+
#[diag(ast_passes_negative_bound_with_parenthetical_notation)]
750+
pub struct NegativeBoundWithParentheticalNotation {
751+
#[primary_span]
752+
pub span: Span,
753+
}
754+
748755
#[derive(Diagnostic)]
749756
#[diag(ast_passes_invalid_unnamed_field_ty)]
750757
pub struct InvalidUnnamedFieldTy {

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

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

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

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ error: associated type constraints not allowed on negative bounds
2222
LL | fn test4<T>() where T: !Trait<Assoc: Send> {}
2323
| ^^^^^^^^^^^
2424

25-
error: aborting due to 4 previous errors
25+
error: parenthetical notation may not be used for negative bounds
26+
--> $DIR/associated-constraints.rs:19:25
27+
|
28+
LL | fn test5<T>() where T: !Fn() -> i32 {}
29+
| ^^^^^^^^^^^
30+
31+
error: aborting due to 5 previous errors
32+

0 commit comments

Comments
 (0)