Skip to content

Commit 86f50b9

Browse files
Disallow associated type constraints on negative bounds
1 parent 6e01e91 commit 86f50b9

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,6 @@ ast_passes_show_span = {$msg}
238238
239239
ast_passes_negative_bound_not_supported =
240240
negative bounds are not supported
241+
242+
ast_passes_constraint_on_negative_bound =
243+
associated type constraints not allowed on negative bounds

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,18 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11771177
}
11781178
}
11791179

1180+
// Negative trait bounds are not allowed to have associated constraints
1181+
if let GenericBound::Trait(trait_ref, TraitBoundModifier::Negative) = bound
1182+
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
1183+
&& let Some(ast::GenericArgs::AngleBracketed(args)) = segment.args.as_deref()
1184+
{
1185+
for arg in &args.args {
1186+
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
1187+
self.err_handler().emit_err(errors::ConstraintOnNegativeBound { span: constraint.span });
1188+
}
1189+
}
1190+
}
1191+
11801192
visit::walk_param_bound(self, bound)
11811193
}
11821194

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,10 @@ pub struct NegativeBoundUnsupported {
701701
#[primary_span]
702702
pub span: Span,
703703
}
704+
705+
#[derive(Diagnostic)]
706+
#[diag(ast_passes_constraint_on_negative_bound)]
707+
pub struct ConstraintOnNegativeBound {
708+
#[primary_span]
709+
pub span: Span,
710+
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
701701

702702
let mut dup_bindings = FxHashMap::default();
703703
for binding in &assoc_bindings {
704-
// TODO: negative polarity can't have associated type bindings!
705-
706704
// Specify type to assert that error was already reported in `Err` case.
707705
let _: Result<_, ErrorGuaranteed> = self.add_predicates_for_ast_type_binding(
708706
hir_id,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(negative_bounds, associated_type_bounds)]
2+
//~^ WARN the feature `negative_bounds` is incomplete and may not be safe to use and/or cause compiler crashes
3+
4+
trait Trait {
5+
type Assoc;
6+
}
7+
8+
fn test<T: !Trait<Assoc = i32>>() {}
9+
//~^ ERROR associated type constraints not allowed on negative bounds
10+
11+
fn test2<T>() where T: !Trait<Assoc = i32> {}
12+
//~^ ERROR associated type constraints not allowed on negative bounds
13+
14+
fn test3<T: !Trait<Assoc: Send>>() {}
15+
//~^ ERROR associated type constraints not allowed on negative bounds
16+
17+
fn test4<T>() where T: !Trait<Assoc: Send> {}
18+
//~^ ERROR associated type constraints not allowed on negative bounds
19+
20+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: associated type constraints not allowed on negative bounds
2+
--> $DIR/associated-constraints.rs:8:19
3+
|
4+
LL | fn test<T: !Trait<Assoc = i32>>() {}
5+
| ^^^^^^^^^^^
6+
7+
error: associated type constraints not allowed on negative bounds
8+
--> $DIR/associated-constraints.rs:11:31
9+
|
10+
LL | fn test2<T>() where T: !Trait<Assoc = i32> {}
11+
| ^^^^^^^^^^^
12+
13+
error: associated type constraints not allowed on negative bounds
14+
--> $DIR/associated-constraints.rs:14:20
15+
|
16+
LL | fn test3<T: !Trait<Assoc: Send>>() {}
17+
| ^^^^^^^^^^^
18+
19+
error: associated type constraints not allowed on negative bounds
20+
--> $DIR/associated-constraints.rs:17:31
21+
|
22+
LL | fn test4<T>() where T: !Trait<Assoc: Send> {}
23+
| ^^^^^^^^^^^
24+
25+
warning: the feature `negative_bounds` is incomplete and may not be safe to use and/or cause compiler crashes
26+
--> $DIR/associated-constraints.rs:1:12
27+
|
28+
LL | #![feature(negative_bounds, associated_type_bounds)]
29+
| ^^^^^^^^^^^^^^^
30+
|
31+
= note: `#[warn(incomplete_features)]` on by default
32+
33+
error: aborting due to 4 previous errors; 1 warning emitted
34+

0 commit comments

Comments
 (0)