Skip to content

Conversation

oriongonza
Copy link
Contributor

@oriongonza oriongonza commented Nov 29, 2024

closes #111229

Now mem::transmute warns when transmuting from &T -> &UnsafeCell<T> (or things that contain it).
In reality it warns when transmuting &impl Freeze -> &impl !Freeze so it might also warn against things like extern types and so on.

  • I've created a new lint: transmute_to_interior_mutability (please check this as I'm not sure if I've done it properly)
  • I've refactored how the mutable_transmutes lint works
  • I've reused the same function as mutable_transmutes so that we can reuse most of the work.
  • notice that in tests/ui/consts/const-eval/issue-55541.rs I've silenced the error... Should we do something about interior mutability and extern types?
  • This leaves the door open for more checks in mem::transmute

Please review the lint text carefully as I don't know how factual that is.
Also, should we do this with raw pointers too? and if so, which cases are worth covering?

@rustbot
Copy link
Collaborator

rustbot commented Nov 29, 2024

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 29, 2024
@oriongonza oriongonza changed the title Improve mut transmute mem::transmute warns if transmuting to a type with interior mutability Nov 29, 2024
@rust-log-analyzer

This comment has been minimized.

@oriongonza oriongonza force-pushed the improve-mut-transmute branch from f771457 to 053455f Compare November 29, 2024 23:23
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#17 exporting to docker image format
#17 sending tarball 28.2s done
#17 DONE 33.9s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
failures:

---- [ui] tests/ui/consts/const-eval/issue-55541.rs stdout ----

error: /checkout/tests/ui/consts/const-eval/issue-55541.rs:6: unexpected warning: '6:10: 6:42: unknown lint: `transmute_to_interior_mutability` [unknown_lints]'

error: /checkout/tests/ui/consts/const-eval/issue-55541.rs:19: unexpected warning: '19:3: 19:22: transmuting from a type without interior mutability to a type with interior mutability is undefined behaviour if you modify the content [transmute_to_interior_mutability]'

error: /checkout/tests/ui/consts/const-eval/issue-55541.rs:25: unexpected warning: '25:3: 25:22: transmuting from a type without interior mutability to a type with interior mutability is undefined behaviour if you modify the content [transmute_to_interior_mutability]'
error: 3 unexpected errors found, 0 expected errors not found
status: exit status: 0
status: exit status: 0
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/consts/const-eval/issue-55541.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/const-eval/issue-55541" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- unexpected errors (from JSON output) ---
--- unexpected errors (from JSON output) ---
WARNING   line   6: 6:10: 6:42: unknown lint: `transmute_to_interior_mutability` [unknown_lints]
WARNING   line  19: 19:3: 19:22: transmuting from a type without interior mutability to a type with interior mutability is undefined behaviour if you modify the content [transmute_to_interior_mutability]
WARNING   line  25: 25:3: 25:22: transmuting from a type without interior mutability to a type with interior mutability is undefined behaviour if you modify the content [transmute_to_interior_mutability]
thread '[ui] tests/ui/consts/const-eval/issue-55541.rs' panicked at src/tools/compiletest/src/runtest.rs:789:13:
errors differ from expected
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@samueltardieu
Copy link
Member

Duplicate of #128351 which has disposition-merge already.

@jieyouxu
Copy link
Member

jieyouxu commented Dec 1, 2024

Hi @dev-ardi, sorry for the confusion, I mistakenly unassigned the author of the already existing PR #128351 on #111229 (I think at the time I didn't connect the two together because there was no backlinks). Thank you for the PR!

@jieyouxu jieyouxu closed this Dec 1, 2024
@oriongonza
Copy link
Contributor Author

Since #128351 got closed could we look into reopening this?

This is a strict subset of the linting of the other PR but way simpler since it uses the is_freezing query, so it could work until the other, more complex lint is completed and merged.

@jieyouxu
Copy link
Member

jieyouxu commented Jul 4, 2025

Apparently there's a rebased version (#143343). I'm not sure what's the intended approach for this particular lint.

I left a comment on that PR referencing this PR, but I'll delegate to that PR's reviewer re. the approach, since I don't have the bandwidth to look into this lint at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mutable_transmutes lint should catch transmutes from a type without interior mutability to one with
6 participants