about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2019-04-17 10:24:50 -0700
committerEsteban Küber <esteban@kuber.com.ar>2019-04-18 19:18:26 -0700
commit6aa4c992bc8ee003be01bfc59d2d7b54d01bc131 (patch)
tree4313d42306b5b29b60ab686db1880e4b8f1ba69b /src/test
parente928e9441157f63a776ba1f8773818838e0912ea (diff)
downloadrust-6aa4c992bc8ee003be01bfc59d2d7b54d01bc131.tar.gz
rust-6aa4c992bc8ee003be01bfc59d2d7b54d01bc131.zip
Suggest appropriate path when calling associated item on bare types
When looking at the documentation for `std::f32` or `std::str`, for
example, it is easy to get confused and assume `std::f32` and `f32`
are the same thing. Because of this, it is not uncommon to attempt
writing `f32::consts::PI` instead of the correct
`std::f32::consts::PI`. When encountering the former, which results
in an access error due to it being an inexistent path, try to access
the same path under `std`. If this succeeds, this information is
stored for later tweaking of the final E0599 to provide an
appropriate suggestion.

This suggestion applies to both E0233 and E0599 and is only checked
when the first ident of a path corresponds to a primitive type.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/issues/issue-22933-3.stderr4
-rw-r--r--src/test/ui/suggestions/suggest-std-when-using-type.rs7
-rw-r--r--src/test/ui/suggestions/suggest-std-when-using-type.stderr24
3 files changed, 35 insertions, 0 deletions
diff --git a/src/test/ui/issues/issue-22933-3.stderr b/src/test/ui/issues/issue-22933-3.stderr
index b1afda6d151..e0e8b5e18a4 100644
--- a/src/test/ui/issues/issue-22933-3.stderr
+++ b/src/test/ui/issues/issue-22933-3.stderr
@@ -3,6 +3,10 @@ error[E0599]: no associated item named `MIN` found for type `u8` in the current
    |
 LL | const FOO: [u32; u8::MIN as usize] = [];
    |                      ^^^ associated item not found in `u8`
+help: you are looking for the module in `std`, not the primitive type
+   |
+LL | const FOO: [u32; std::u8::MIN as usize] = [];
+   |                  ^^^^^^^^^^^^
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/suggestions/suggest-std-when-using-type.rs b/src/test/ui/suggestions/suggest-std-when-using-type.rs
new file mode 100644
index 00000000000..9ca68a635da
--- /dev/null
+++ b/src/test/ui/suggestions/suggest-std-when-using-type.rs
@@ -0,0 +1,7 @@
+fn main() {
+    let pi = f32::consts::PI; //~ ERROR ambiguous associated type
+    let bytes = "hello world".as_bytes();
+    let string = unsafe {
+        str::from_utf8(bytes) //~ ERROR no function or associated item named `from_utf8` found
+    };
+}
diff --git a/src/test/ui/suggestions/suggest-std-when-using-type.stderr b/src/test/ui/suggestions/suggest-std-when-using-type.stderr
new file mode 100644
index 00000000000..eecb4e60f9d
--- /dev/null
+++ b/src/test/ui/suggestions/suggest-std-when-using-type.stderr
@@ -0,0 +1,24 @@
+error[E0223]: ambiguous associated type
+  --> $DIR/suggest-std-when-using-type.rs:2:14
+   |
+LL |     let pi = f32::consts::PI;
+   |              ^^^^^^^^^^^^^^^
+help: you are looking for the module in `std`, not the primitive type
+   |
+LL |     let pi = std::f32::consts::PI;
+   |              ^^^^^^^^^^^^^^^^^^^^
+
+error[E0599]: no function or associated item named `from_utf8` found for type `str` in the current scope
+  --> $DIR/suggest-std-when-using-type.rs:5:14
+   |
+LL |         str::from_utf8(bytes)
+   |              ^^^^^^^^^ function or associated item not found in `str`
+help: you are looking for the module in `std`, not the primitive type
+   |
+LL |         std::str::from_utf8(bytes)
+   |         ^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0223, E0599.
+For more information about an error, try `rustc --explain E0223`.