Skip to content

Require #[pin_v2] for direct &pin borrows of ADTs#2

Merged
frank-king merged 1 commit intofrank-king:feature/pin-borrowckfrom
P8L1:enforce-pin-v2-for-direct-pin-borrows
Apr 29, 2026
Merged

Require #[pin_v2] for direct &pin borrows of ADTs#2
frank-king merged 1 commit intofrank-king:feature/pin-borrowckfrom
P8L1:enforce-pin-v2-for-direct-pin-borrows

Conversation

@P8L1
Copy link
Copy Markdown

@P8L1 P8L1 commented Apr 28, 2026

Summary

Require direct user-written &pin borrows of ADTs to use #[pin_v2].

The check runs during HIR type checking for explicit hir::BorrowKind::Pin, so it
rejects direct &pin mut / &pin const borrow expressions on non-#[pin_v2]
ADTs without affecting compiler-generated AutoBorrow::Pin adjustments or
coercions.

What changed

  • Added a direct-borrow diagnostic for attempting to directly pin an ADT that is
    not #[pin_v2].
  • Added a typeck check in FnCtxt::check_expr_addr_of for explicit user-written
    &pin borrows.
  • Reused AdtDef::is_pin_project() as the source of truth for #[pin_v2].
  • Updated existing pin ergonomics tests that should continue reaching borrowck by
    marking their local ADTs #[pin_v2].
  • Added focused UI coverage for direct &pin mut and &pin const borrows of a
    non-#[pin_v2] ADT.

Why

The active pin ergonomics borrowck PR still had an unchecked task to forbid
&pin borrows for ADTs without #[pin_v2].

Pattern projection already rejects non-#[pin_v2] ADTs in related cases, but
direct user-written &pin mut foo and &pin const foo borrow expressions were
still accepted.

This PR enforces that direct rule while avoiding compiler-generated transient pin
adjustments.

Tests

Passed:

./x test tests/ui/pin-ergonomics/direct-borrow-requires-pin-v2.rs --stage 1 --test-args --force-rerun
./x test tests/ui/pin-ergonomics/borrow.rs --stage 1 --test-args --force-rerun
./x test tests/ui/pin-ergonomics/borrow-unpin.rs --stage 1 --test-args --force-rerun
./x test tests/ui/pin-ergonomics/borrow-pinned-projection.rs --stage 1 --test-args --force-rerun
./x test tests/ui/pin-ergonomics/user-type-projection.rs --stage 1 --test-args --force-rerun
./x test tests/ui/pin-ergonomics/pin_v2-attr.rs --stage 1
./x test tests/ui/pin-ergonomics/impl-unpin.rs --stage 1
./x test tests/ui/pin-ergonomics/pattern-matching.rs --stage 1
./x test tests/ui/pin-ergonomics/pattern-matching-mix-deref-pattern.rs --stage 1
./x test tests/ui/pin-ergonomics/pin-coercion.rs --stage 1
./x test tests/ui/pin-ergonomics/pin-coercion-unpin-roundtrip.rs --stage 1
./x test tests/ui/pin-ergonomics/pin-coercion-get-mut-then-as-mut.rs --stage 1
git diff --check
./x fmt --check

Notes for reviewers

The main review questions are:

  • Should generic type parameters remain accepted by this direct ADT-only rule?
  • Should direct &pin of the Pin ADT itself have any special exception, or
    should the literal ADT rule apply?
  • Is the machine-applicable #[pin_v2] suggestion appropriate here, matching
    the existing projection diagnostic style?

Related

@P8L1
Copy link
Copy Markdown
Author

P8L1 commented Apr 28, 2026

The CI failure appears to come from src/ci/scripts/verify-channel.sh because this PR targets the feature/pin-borrowck branch rather than the normal Rust main branch.

The focused local checks passed and are listed in the PR body. This PR is intended as a patch against the active pin-borrowck feature branch for rust-lang#153693, not as an independent PR directly to main.

@frank-king
Copy link
Copy Markdown
Owner

Grateful for your help! Let's merge it first and see if the original PR can pass the CI.

@frank-king frank-king merged commit d7f4bbd into frank-king:feature/pin-borrowck Apr 29, 2026
2 of 11 checks passed
@P8L1
Copy link
Copy Markdown
Author

P8L1 commented Apr 29, 2026

I saw the CI failure in https://github.com/rust-lang/rust/actions/runs/25086407095/job/73502863053 after my merged helper PR.

I’m reproducing the exact failing pin-coercion.rs path against the current feature/pin-borrowck branch now, including the stage-2 test path, before making any further changes.

If this was caused by my patch, I’ll follow up with a focused fix PR. Sorry for the churn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants