about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorJana Dönszelmann <jonathan@donsz.nl>2025-07-03 13:29:36 +0200
committerGitHub <noreply@github.com>2025-07-03 13:29:36 +0200
commit0aaac883de0b16b6ec60b48038913e05eff5cc2f (patch)
tree27d85f8268290ae3d9a9951d3ae0152e7f04e526 /tests
parent5026d0cd8e45d2c882d1161edcb6c40e97c87a1a (diff)
parent6b824e8143c1dcacdbac3f14f01e2bbb85da8907 (diff)
downloadrust-0aaac883de0b16b6ec60b48038913e05eff5cc2f.tar.gz
rust-0aaac883de0b16b6ec60b48038913e05eff5cc2f.zip
Rollup merge of #143038 - Qelxiros:142676-private-dependency-traits, r=tgross35
avoid suggesting traits from private dependencies

fixes rust-lang/rust#142676
fixes rust-lang/rust#138191

r? ``@tgross35``
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/typeck/auxiliary/private-dep.rs3
-rw-r--r--tests/ui/typeck/auxiliary/public-dep.rs11
-rw-r--r--tests/ui/typeck/dont-suggest-private-dependencies.rs37
-rw-r--r--tests/ui/typeck/dont-suggest-private-dependencies.stderr27
4 files changed, 78 insertions, 0 deletions
diff --git a/tests/ui/typeck/auxiliary/private-dep.rs b/tests/ui/typeck/auxiliary/private-dep.rs
new file mode 100644
index 00000000000..472b40ef622
--- /dev/null
+++ b/tests/ui/typeck/auxiliary/private-dep.rs
@@ -0,0 +1,3 @@
+pub trait A {
+    fn foo() {}
+}
diff --git a/tests/ui/typeck/auxiliary/public-dep.rs b/tests/ui/typeck/auxiliary/public-dep.rs
new file mode 100644
index 00000000000..438692a1caa
--- /dev/null
+++ b/tests/ui/typeck/auxiliary/public-dep.rs
@@ -0,0 +1,11 @@
+//@ aux-crate:priv:private_dep=private-dep.rs
+//@ compile-flags: -Zunstable-options
+
+extern crate private_dep;
+use private_dep::A;
+
+pub struct B;
+
+impl A for B {
+    fn foo() {}
+}
diff --git a/tests/ui/typeck/dont-suggest-private-dependencies.rs b/tests/ui/typeck/dont-suggest-private-dependencies.rs
new file mode 100644
index 00000000000..ee5224e2d82
--- /dev/null
+++ b/tests/ui/typeck/dont-suggest-private-dependencies.rs
@@ -0,0 +1,37 @@
+// Don't suggest importing a function from a private dependency.
+// Issues: #138191, #142676
+
+// Avoid suggesting traits from std-private deps
+//@ forbid-output: compiler_builtins
+//@ forbid-output: object
+
+// Check a custom trait to withstand changes in above crates
+//@ aux-crate:public_dep=public-dep.rs
+//@ compile-flags: -Zunstable-options
+//@ forbid-output: private_dep
+
+// By default, the `read` diagnostic suggests `std::os::unix::fs::FileExt::read_at`. Add
+// something more likely to be recommended to make the diagnostic cross-platform.
+trait DecoyRead {
+    fn read1(&self) {}
+}
+impl<T> DecoyRead for Vec<T> {}
+
+struct VecReader(Vec<u8>);
+
+impl std::io::Read for VecReader {
+    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+        self.0.read(buf)
+        //~^ ERROR no method named `read` found for struct `Vec<u8>`
+    }
+}
+
+extern crate public_dep;
+use public_dep::B;
+
+fn main() {
+    let _ = u8::cast_from_lossy(9);
+    //~^ ERROR no function or associated item named `cast_from_lossy` found for type `u8`
+    let _ = B::foo();
+    //~^ ERROR no function or associated item named `foo` found for struct `B`
+}
diff --git a/tests/ui/typeck/dont-suggest-private-dependencies.stderr b/tests/ui/typeck/dont-suggest-private-dependencies.stderr
new file mode 100644
index 00000000000..b7b14ee6b9b
--- /dev/null
+++ b/tests/ui/typeck/dont-suggest-private-dependencies.stderr
@@ -0,0 +1,27 @@
+error[E0599]: no method named `read` found for struct `Vec<u8>` in the current scope
+  --> $DIR/dont-suggest-private-dependencies.rs:24:16
+   |
+LL |         self.0.read(buf)
+   |                ^^^^
+   |
+help: there is a method `read1` with a similar name, but with different arguments
+  --> $DIR/dont-suggest-private-dependencies.rs:16:5
+   |
+LL |     fn read1(&self) {}
+   |     ^^^^^^^^^^^^^^^
+
+error[E0599]: no function or associated item named `cast_from_lossy` found for type `u8` in the current scope
+  --> $DIR/dont-suggest-private-dependencies.rs:33:17
+   |
+LL |     let _ = u8::cast_from_lossy(9);
+   |                 ^^^^^^^^^^^^^^^ function or associated item not found in `u8`
+
+error[E0599]: no function or associated item named `foo` found for struct `B` in the current scope
+  --> $DIR/dont-suggest-private-dependencies.rs:35:16
+   |
+LL |     let _ = B::foo();
+   |                ^^^ function or associated item not found in `B`
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0599`.