Skip to content

Bad path to casted type (ptr_as_ptr) causing use of undeclared crate or module #11278

@madonuko

Description

@madonuko

Summary

Seems to be related to #10854.

When clippy tries to fix ptr_as_ptr, it uses the .cast::<T>() function with the wrong path to T.

Reproducer

#![warn(clippy::pedantic)]
mod a {
	#[derive(Debug)]
	pub struct T<D: std::fmt::Debug + ?Sized> {
		pub p: D,
	}
}

mod b {
	pub fn f(o: &mut super::a::T<dyn std::fmt::Debug>) -> super::a::T<String> {
		*unsafe { Box::from_raw(Box::into_raw(Box::new(o)) as *mut super::a::T<String>) }
	}
}

fn main() {
	let x = b::f(&mut a::T { p: String::new() });
	dbg!(x);
}

$ cargo clippy --fix

warning: failed to automatically apply fixes suggested by rustc to crate `dlkfjalskjflasdjf`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see 
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0433]: failed to resolve: use of undeclared crate or module `a`
  --> src/main.rs:11:61
   |
11 |         *unsafe { Box::from_raw(Box::into_raw(Box::new(o)).cast::<a::T<std::string::String>>()) }
   |                                                                   ^ use of undeclared crate or module `a`
   |
help: there is a crate or module with a similar name
   |
11 |         *unsafe { Box::from_raw(Box::into_raw(Box::new(o)).cast::<b::T<std::string::String>>()) }
   |                                                                   ~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
Original diagnostics will follow.

warning: `as` casting between raw pointers without changing its mutability
  --> src/main.rs:11:27
   |
11 |         *unsafe { Box::from_raw(Box::into_raw(Box::new(o)) as *mut super::a::T<String>) }
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `Box::into_raw(Box::new(o)).cast::<a::T<std::string::String>>()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::pedantic)]
   |         ^^^^^^^^^^^^^^^^
   = note: `#[warn(clippy::ptr_as_ptr)]` implied by `#[warn(clippy::pedantic)]`

warning: `dlkfjalskjflasdjf` (bin "dlkfjalskjflasdjf") generated 1 warning (run `cargo clippy --fix --bin "dlkfjalskjflasdjf"` to apply 1 suggestion)
warning: `dlkfjalskjflasdjf` (bin "dlkfjalskjflasdjf" test) generated 1 warning (1 duplicate)

Version

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-unknown-linux-gnu
release: 1.71.0
LLVM version: 16.0.5

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions