rust/tests/ui/object-safety
Matthias Krüger a88354831b
Rollup merge of #126090 - compiler-errors:supertrait-assoc-ty-unsoundness, r=lcnr
Fix supertrait associated type unsoundness

### What?

Object safety allows us to name `Self::Assoc` associated types in certain positions if they come from our trait or one of our supertraits. When this check was implemented, I think it failed to consider that supertraits can have different args, and it was only checking def-id equality.

This is problematic, since we can sneak different implementations in by implementing `Supertrait<NotActuallyTheSupertraitSubsts>` for a `dyn` type. This can be used to implement an unsound transmute function. See the committed test.

### How do we fix it?

We consider the whole trait ref when checking for supertraits. Right now, this is implemented using equality *without* normalization. We erase regions since those don't affect trait selection.

This is a limitation that could theoretically affect code that should be accepted, but doesn't matter in practice -- there are 0 crater regression. We could make this check stronger, but I would be worried about cycle issues. I assume that most people are writing `Self::Assoc` so they don't really care about the trait ref being normalized.

---

### What is up w the stacked commit

This is built on top of https://github.com/rust-lang/rust/pull/122804 though that's really not related, it's just easier to make this modification with the changes to the object safety code that I did in that PR. The only thing is that PR may make this unsoundness slightly easier to abuse, since there are more positions that allow self-associated-types -- I am happy to stall that change until this PR merges.

---

Fixes #126079

r? lcnr
2024-07-26 00:57:20 +02:00
..
almost-supertrait-associated-type.rs Fix unsoundness when associated types dont actually come from supertraits 2024-07-15 14:17:32 -04:00
almost-supertrait-associated-type.stderr Fix unsoundness when associated types dont actually come from supertraits 2024-07-15 14:17:32 -04:00
assoc_const_bounds_sized.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
assoc_const_bounds.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
assoc_type_bounds2.rs Detect object safety errors when assoc type is missing 2023-10-30 22:12:07 +00:00
assoc_type_bounds2.stderr Detect object safety errors when assoc type is missing 2023-10-30 22:12:07 +00:00
assoc_type_bounds_implicit_sized.fixed [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
assoc_type_bounds_implicit_sized.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
assoc_type_bounds_implicit_sized.stderr Update tests 2024-02-07 10:42:01 +08:00
assoc_type_bounds_sized_others.rs Detect object safety errors when assoc type is missing 2023-10-30 22:12:07 +00:00
assoc_type_bounds_sized_others.stderr Detect object safety errors when assoc type is missing 2023-10-30 22:12:07 +00:00
assoc_type_bounds_sized_unnecessary.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
assoc_type_bounds_sized_unnecessary.stderr Fix remaining cases 2024-06-21 19:00:18 -04:00
assoc_type_bounds_sized_used.rs Do not require associated types with Self: Sized to uphold bounds when confirming object candidate 2023-09-02 05:08:38 +00:00
assoc_type_bounds_sized_used.stderr Suggest relaxing implicit type Assoc: Sized; bound 2023-10-19 00:07:16 +00:00
assoc_type_bounds_sized.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
assoc_type_bounds.rs Detect object safety errors when assoc type is missing 2023-10-30 22:12:07 +00:00
assoc_type_bounds.stderr Detect object safety errors when assoc type is missing 2023-10-30 22:12:07 +00:00
avoid-ice-on-warning-2.new.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
avoid-ice-on-warning-2.old.stderr Auto merge of #120847 - oli-obk:track_errors9, r=compiler-errors 2024-02-14 18:32:19 +00:00
avoid-ice-on-warning-2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
avoid-ice-on-warning-3.new.stderr Avoid ICE in trait without dyn lint 2024-01-29 18:33:52 +00:00
avoid-ice-on-warning-3.old.stderr Be less confident when dyn suggestion is not checked for object safety 2024-02-09 20:47:50 -08:00
avoid-ice-on-warning-3.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
avoid-ice-on-warning.new.stderr Make parse error suggestions verbose and fix spans 2024-07-12 03:02:57 +00:00
avoid-ice-on-warning.old.stderr Make parse error suggestions verbose and fix spans 2024-07-12 03:02:57 +00:00
avoid-ice-on-warning.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
bare-trait-dont-suggest-dyn.new.fixed tests/ui: remove workaround for broken revisioned run-rustfix test 2024-04-07 17:06:15 +00:00
bare-trait-dont-suggest-dyn.new.stderr tests/ui: remove workaround for broken revisioned run-rustfix test 2024-04-07 17:06:15 +00:00
bare-trait-dont-suggest-dyn.old.stderr tests/ui: remove workaround for broken revisioned run-rustfix test 2024-04-07 17:06:15 +00:00
bare-trait-dont-suggest-dyn.rs tests/ui: remove workaround for broken revisioned run-rustfix test 2024-04-07 17:06:15 +00:00
call-when-assoc-ty-is-sized.rs Ignore tests w/ current/next revisions from compare-mode=next-solver 2024-03-10 21:18:41 -04:00
erroneous_signature.rs Don't ICE when deducing future output if other errors already occurred 2024-01-17 16:27:57 +00:00
erroneous_signature.stderr Don't ICE when deducing future output if other errors already occurred 2024-01-17 16:27:57 +00:00
issue-19538.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-19538.stderr On object safety error, mention new enum as alternative 2023-10-29 23:55:46 +00:00
issue-102762.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-102762.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
issue-102933.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-106247.rs Make WHERE_CLAUSES_OBJECT_SAFETY a regular object safety violation 2024-06-03 09:49:04 -04:00
item-bounds-can-reference-self.rs Item bounds can reference self projections and still be object safe 2024-07-15 14:16:48 -04:00
object-safety-associated-consts.curr.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-associated-consts.object_safe_for_dispatch.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
object-safety-associated-consts.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-bounds.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
object-safety-bounds.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
object-safety-by-value-self-use.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
object-safety-by-value-self-use.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
object-safety-by-value-self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-generics.curr.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-generics.object_safe_for_dispatch.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-generics.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-issue-22040.rs Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-issue-22040.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-mentions-Self.curr.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-mentions-Self.object_safe_for_dispatch.stderr Note base types of coercion 2023-05-12 00:10:52 +00:00
object-safety-mentions-Self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-no-static.curr.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-no-static.object_safe_for_dispatch.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
object-safety-no-static.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-phantom-fn.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-sized-2.curr.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-sized-2.object_safe_for_dispatch.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
object-safety-sized-2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-sized.curr.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-sized.object_safe_for_dispatch.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
object-safety-sized.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-supertrait-mentions-GAT.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
object-safety-supertrait-mentions-GAT.stderr always show and explain sub region 2023-10-08 09:59:51 +00:00
object-safety-supertrait-mentions-Self.rs Avoid silencing relevant follow-up errors 2024-01-09 21:08:16 +00:00
object-safety-supertrait-mentions-Self.stderr Merge collect_mod_item_types query into check_well_formed 2024-03-07 14:26:31 +00:00