Deny parenthetical notation for negative bounds

This commit is contained in:
León Orell Valerian Liehr 2023-12-27 17:57:19 +01:00
parent 5f56465621
commit a251974015
No known key found for this signature in database
GPG Key ID: D17A07215F68E713
5 changed files with 37 additions and 6 deletions

View File

@ -188,6 +188,9 @@ ast_passes_module_nonascii = trying to load file for module `{$name}` with non-a
ast_passes_negative_bound_not_supported = ast_passes_negative_bound_not_supported =
negative bounds are not supported negative bounds are not supported
ast_passes_negative_bound_with_parenthetical_notation =
parenthetical notation may not be used for negative bounds
ast_passes_nested_impl_trait = nested `impl Trait` is not allowed ast_passes_nested_impl_trait = nested `impl Trait` is not allowed
.outer = outer `impl Trait` .outer = outer `impl Trait`
.inner = nested `impl Trait` here .inner = nested `impl Trait` here

View File

@ -1257,15 +1257,26 @@ fn visit_param_bound(&mut self, bound: &'a GenericBound, ctxt: BoundKind) {
if let GenericBound::Trait(trait_ref, modifiers) = bound if let GenericBound::Trait(trait_ref, modifiers) = bound
&& let BoundPolarity::Negative(_) = modifiers.polarity && let BoundPolarity::Negative(_) = modifiers.polarity
&& let Some(segment) = trait_ref.trait_ref.path.segments.last() && let Some(segment) = trait_ref.trait_ref.path.segments.last()
&& let Some(ast::GenericArgs::AngleBracketed(args)) = segment.args.as_deref()
{ {
match segment.args.as_deref() {
Some(ast::GenericArgs::AngleBracketed(args)) => {
for arg in &args.args { for arg in &args.args {
if let ast::AngleBracketedArg::Constraint(constraint) = arg { if let ast::AngleBracketedArg::Constraint(constraint) = arg {
self.dcx() self.dcx().emit_err(errors::ConstraintOnNegativeBound {
.emit_err(errors::ConstraintOnNegativeBound { span: constraint.span }); span: constraint.span,
});
} }
} }
} }
// The lowered form of parenthesized generic args contains a type binding.
Some(ast::GenericArgs::Parenthesized(args)) => {
self.dcx().emit_err(errors::NegativeBoundWithParentheticalNotation {
span: args.span,
});
}
None => {}
}
}
visit::walk_param_bound(self, bound) visit::walk_param_bound(self, bound)
} }

View File

@ -745,6 +745,13 @@ pub struct ConstraintOnNegativeBound {
pub span: Span, pub span: Span,
} }
#[derive(Diagnostic)]
#[diag(ast_passes_negative_bound_with_parenthetical_notation)]
pub struct NegativeBoundWithParentheticalNotation {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(ast_passes_invalid_unnamed_field_ty)] #[diag(ast_passes_invalid_unnamed_field_ty)]
pub struct InvalidUnnamedFieldTy { pub struct InvalidUnnamedFieldTy {

View File

@ -16,4 +16,7 @@ fn test3<T: !Trait<Assoc: Send>>() {}
fn test4<T>() where T: !Trait<Assoc: Send> {} fn test4<T>() where T: !Trait<Assoc: Send> {}
//~^ ERROR associated type constraints not allowed on negative bounds //~^ ERROR associated type constraints not allowed on negative bounds
fn test5<T>() where T: !Fn() -> i32 {}
//~^ ERROR parenthetical notation may not be used for negative bounds
fn main() {} fn main() {}

View File

@ -22,4 +22,11 @@ error: associated type constraints not allowed on negative bounds
LL | fn test4<T>() where T: !Trait<Assoc: Send> {} LL | fn test4<T>() where T: !Trait<Assoc: Send> {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: aborting due to 4 previous errors error: parenthetical notation may not be used for negative bounds
--> $DIR/associated-constraints.rs:19:25
|
LL | fn test5<T>() where T: !Fn() -> i32 {}
| ^^^^^^^^^^^
error: aborting due to 5 previous errors