Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make negative_bounds internal & fix some of its issues #119354

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ ast_passes_module_nonascii = trying to load file for module `{$name}` with non-a
ast_passes_negative_bound_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
.outer = outer `impl Trait`
.inner = nested `impl Trait` here
Expand Down
21 changes: 16 additions & 5 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,13 +1257,24 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
if let GenericBound::Trait(trait_ref, modifiers) = bound
&& let BoundPolarity::Negative(_) = modifiers.polarity
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
&& let Some(ast::GenericArgs::AngleBracketed(args)) = segment.args.as_deref()
{
for arg in &args.args {
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
self.dcx()
.emit_err(errors::ConstraintOnNegativeBound { span: constraint.span });
match segment.args.as_deref() {
Some(ast::GenericArgs::AngleBracketed(args)) => {
for arg in &args.args {
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
self.dcx().emit_err(errors::ConstraintOnNegativeBound {
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 => {}
}
}

Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,13 @@ pub struct ConstraintOnNegativeBound {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_negative_bound_with_parenthetical_notation)]
pub struct NegativeBoundWithParentheticalNotation {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_invalid_unnamed_field_ty)]
pub struct InvalidUnnamedFieldTy {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ declare_features! (
/// Allows the `multiple_supertrait_upcastable` lint.
(unstable, multiple_supertrait_upcastable, "1.69.0", None),
/// Allow negative trait bounds. This is an internal-only feature for testing the trait solver!
(incomplete, negative_bounds, "1.71.0", None),
(internal, negative_bounds, "1.71.0", None),
/// Allows using `#[omit_gdb_pretty_printer_section]`.
(internal, omit_gdb_pretty_printer_section, "1.5.0", None),
/// Allows using `#[prelude_import]` on glob `use` items.
Expand Down
52 changes: 30 additions & 22 deletions compiler/rustc_hir_analysis/src/astconv/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,36 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
span: Span,
) {
let tcx = self.tcx();
let sized_def_id = tcx.lang_items().sized_trait();
let mut seen_negative_sized_bound = false;

// Try to find an unbound in bounds.
let mut unbounds: SmallVec<[_; 1]> = SmallVec::new();
let mut search_bounds = |ast_bounds: &'tcx [hir::GenericBound<'tcx>]| {
for ab in ast_bounds {
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) = ab {
unbounds.push(ptr)
let hir::GenericBound::Trait(ptr, modifier) = ab else {
continue;
};
match modifier {
hir::TraitBoundModifier::Maybe => unbounds.push(ptr),
hir::TraitBoundModifier::Negative => {
if let Some(sized_def_id) = sized_def_id
&& ptr.trait_ref.path.res == Res::Def(DefKind::Trait, sized_def_id)
{
seen_negative_sized_bound = true;
}
}
_ => {}
}
}
};
search_bounds(ast_bounds);
if let Some((self_ty, where_clause)) = self_ty_where_predicates {
for clause in where_clause {
if let hir::WherePredicate::BoundPredicate(pred) = clause {
if pred.is_param_bound(self_ty.to_def_id()) {
search_bounds(pred.bounds);
}
if let hir::WherePredicate::BoundPredicate(pred) = clause
&& pred.is_param_bound(self_ty.to_def_id())
{
search_bounds(pred.bounds);
}
}
}
Expand All @@ -53,15 +66,13 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
});
}

let sized_def_id = tcx.lang_items().sized_trait();

let mut seen_sized_unbound = false;
for unbound in unbounds {
if let Some(sized_def_id) = sized_def_id {
if unbound.trait_ref.path.res == Res::Def(DefKind::Trait, sized_def_id) {
seen_sized_unbound = true;
continue;
}
if let Some(sized_def_id) = sized_def_id
&& unbound.trait_ref.path.res == Res::Def(DefKind::Trait, sized_def_id)
{
seen_sized_unbound = true;
continue;
}
// There was a `?Trait` bound, but it was not `?Sized`; warn.
tcx.dcx().span_warn(
Expand All @@ -71,15 +82,12 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
);
}

// If the above loop finished there was no `?Sized` bound; add implicitly sized if `Sized` is available.
if sized_def_id.is_none() {
// No lang item for `Sized`, so we can't add it as a bound.
return;
}
if seen_sized_unbound {
// There was in fact a `?Sized` bound, return without doing anything
} else {
// There was no `?Sized` bound; add implicitly sized if `Sized` is available.
if seen_sized_unbound || seen_negative_sized_bound {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if I should reject !Sized + ?Sized: (kinda) “redundant ?Sized bound” // “!Sized already opts out of Sized”.

// There was in fact a `?Sized` or `!Sized` bound;
// we don't need to do anything.
} else if sized_def_id.is_some() {
// There was no `?Sized` or `!Sized` bound;
// add `Sized` if it's available.
bounds.push_sized(tcx, self_ty, span);
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/traits/negative-bounds/associated-constraints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(negative_bounds, associated_type_bounds)]
//~^ WARN the feature `negative_bounds` is incomplete and may not be safe to use and/or cause compiler crashes

trait Trait {
type Assoc;
Expand All @@ -17,4 +16,7 @@ fn test3<T: !Trait<Assoc: Send>>() {}
fn test4<T>() where T: !Trait<Assoc: Send> {}
//~^ ERROR associated type constraints not allowed on negative bounds

fn test5<T>() where T: !Fn() -> i32 {}
Copy link
Member Author

@fmease fmease Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one used to ice with no errors encountered even though `span_delayed_bug` issued // negative trait bounds should not have bindings.

//~^ ERROR parenthetical notation may not be used for negative bounds

fn main() {}
20 changes: 9 additions & 11 deletions tests/ui/traits/negative-bounds/associated-constraints.stderr
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
error: associated type constraints not allowed on negative bounds
--> $DIR/associated-constraints.rs:8:19
--> $DIR/associated-constraints.rs:7:19
|
LL | fn test<T: !Trait<Assoc = i32>>() {}
| ^^^^^^^^^^^

error: associated type constraints not allowed on negative bounds
--> $DIR/associated-constraints.rs:11:31
--> $DIR/associated-constraints.rs:10:31
|
LL | fn test2<T>() where T: !Trait<Assoc = i32> {}
| ^^^^^^^^^^^

error: associated type constraints not allowed on negative bounds
--> $DIR/associated-constraints.rs:14:20
--> $DIR/associated-constraints.rs:13:20
|
LL | fn test3<T: !Trait<Assoc: Send>>() {}
| ^^^^^^^^^^^

error: associated type constraints not allowed on negative bounds
--> $DIR/associated-constraints.rs:17:31
--> $DIR/associated-constraints.rs:16:31
|
LL | fn test4<T>() where T: !Trait<Assoc: Send> {}
| ^^^^^^^^^^^

warning: the feature `negative_bounds` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/associated-constraints.rs:1:12
error: parenthetical notation may not be used for negative bounds
--> $DIR/associated-constraints.rs:19:25
|
LL | #![feature(negative_bounds, associated_type_bounds)]
| ^^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default
LL | fn test5<T>() where T: !Fn() -> i32 {}
| ^^^^^^^^^^^

error: aborting due to 4 previous errors; 1 warning emitted
error: aborting due to 5 previous errors

1 change: 0 additions & 1 deletion tests/ui/traits/negative-bounds/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(negative_bounds, negative_impls)]
//~^ WARN the feature `negative_bounds` is incomplete and may not be safe to use and/or cause compiler crashes

fn not_copy<T: !Copy>() {}

Expand Down
26 changes: 9 additions & 17 deletions tests/ui/traits/negative-bounds/simple.stderr
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
warning: the feature `negative_bounds` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/simple.rs:1:12
|
LL | #![feature(negative_bounds, negative_impls)]
| ^^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

error[E0277]: the trait bound `T: !Copy` is not satisfied
--> $DIR/simple.rs:11:16
--> $DIR/simple.rs:10:16
|
LL | not_copy::<T>();
| ^ the trait `!Copy` is not implemented for `T`
|
note: required by a bound in `not_copy`
--> $DIR/simple.rs:4:16
--> $DIR/simple.rs:3:16
|
LL | fn not_copy<T: !Copy>() {}
| ^^^^^ required by this bound in `not_copy`

error[E0277]: the trait bound `T: !Copy` is not satisfied
--> $DIR/simple.rs:16:16
--> $DIR/simple.rs:15:16
|
LL | not_copy::<T>();
| ^ the trait `!Copy` is not implemented for `T`
|
note: required by a bound in `not_copy`
--> $DIR/simple.rs:4:16
--> $DIR/simple.rs:3:16
|
LL | fn not_copy<T: !Copy>() {}
| ^^^^^ required by this bound in `not_copy`

error[E0277]: the trait bound `Copyable: !Copy` is not satisfied
--> $DIR/simple.rs:31:16
--> $DIR/simple.rs:30:16
|
LL | not_copy::<Copyable>();
| ^^^^^^^^ the trait `!Copy` is not implemented for `Copyable`
|
= help: the trait `Copy` is implemented for `Copyable`
note: required by a bound in `not_copy`
--> $DIR/simple.rs:4:16
--> $DIR/simple.rs:3:16
|
LL | fn not_copy<T: !Copy>() {}
| ^^^^^ required by this bound in `not_copy`
Expand All @@ -49,13 +41,13 @@ LL | struct Copyable;
|

error[E0277]: the trait bound `NotNecessarilyCopyable: !Copy` is not satisfied
--> $DIR/simple.rs:38:16
--> $DIR/simple.rs:37:16
|
LL | not_copy::<NotNecessarilyCopyable>();
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `!Copy` is not implemented for `NotNecessarilyCopyable`
|
note: required by a bound in `not_copy`
--> $DIR/simple.rs:4:16
--> $DIR/simple.rs:3:16
|
LL | fn not_copy<T: !Copy>() {}
| ^^^^^ required by this bound in `not_copy`
Expand All @@ -65,6 +57,6 @@ LL + #[derive(Copy)]
LL | struct NotNecessarilyCopyable;
|

error: aborting due to 4 previous errors; 1 warning emitted
error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0277`.
1 change: 0 additions & 1 deletion tests/ui/traits/negative-bounds/supertrait.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// check-pass

#![feature(negative_bounds)]
//~^ WARN the feature `negative_bounds` is incomplete

trait A: !B {}
trait B: !A {}
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/traits/negative-bounds/supertrait.stderr

This file was deleted.