Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages for unresolved imports due to privateness when using use foo::* #6477

Closed
kud1ing opened this issue May 14, 2013 · 15 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@kud1ing
Copy link

kud1ing commented May 14, 2013

In #6443 @cmr made rustc for the following code

mod foo {
   struct Foo;
}

mod bar {
   use foo::Foo;
   struct Bar {
     x : Foo
   }
}

explain error: unresolved import: found "Foo" in "foo" but it is private.

When the module members however are imported using * this way:

mod foo {
   struct Foo;
}

mod bar {
   use foo::*;
   struct Bar {
     x : Foo
   }
}

rustc only says error: use of undeclared type name "Foo".

I think the most prominent use case could be use super::* in unit-test modules.

@emberian
Copy link
Member

This is going to need some deeper changes, I think. Might be a while, I'm not familiar with this part of rustc.

@kud1ing
Copy link
Author

kud1ing commented May 15, 2013

No worries.

@emberian
Copy link
Member

I think the way to implement this is to add, at https://github.com/mozilla/rust/blob/incoming/src/librustc/middle/resolve.rs#L4035, iteration over all external types and checking if they exist or are private. Not really sure how to do this or if it's possible, but I think going through a similar process as (or maybe even just calling) resolve_definition_of_name_in_module with xray could do the trick.

@emberian
Copy link
Member

(that is, check if they have the same name)

@Aatch
Copy link
Contributor

Aatch commented May 23, 2013

A quick study of the code, suggests to me that doing this: (roughly)

let orig_xray = self.xray_context;
self.xray_context = Xray;
match self.resolve_path(path, TypeNS, true, visitor) {
    Some(def) => {
        // Found it, but it's private
    }
    None => {
        // Straight-up doesn't exist
    }
}
let self.xray_context = orig_xray;

Which is similar to your idea. I'm not too familiar with the resolver though, so there may be a better way.

@emberian
Copy link
Member

This doesn't work, unfortunately. Not familiar enough with the resolver to figure out why. The log shows:

rust: ~"(resolving item) resolving Bar"
rust: ~"(resolving item in lexical scope) resolving `Foo` in namespace TypeNS in `bar`"
rust: ~"(resolving item in lexical scope) found import resolution, but not in namespace TypeNS"
rust: ~"\"(resolving item in lexical scope) unresolved module: not searching through module parents\""

though.

@emberian
Copy link
Member

Triage bump. I've made no progress with this.

@kud1ing
Copy link
Author

kud1ing commented Jan 24, 2014

I tend to close this, because of the glob feature gate and the chance of glob imports going away.

@Aatch
Copy link
Contributor

Aatch commented Jan 26, 2014

I opened #11825 to cover glob imports going away, so this is contingent on that issue.

frewsxcv added a commit to frewsxcv/regex that referenced this issue Dec 24, 2014
They're leftovers from the enum namespacing transition

Other than removing public reexports, I also had to publicize a type
(compile::InstIdx). It should have always been public but was no
error was never given because of this bug:

rust-lang/rust#6477

I also had to enable documentation for compile::Ist because it was
complaining that there wasn't any.
frewsxcv added a commit to frewsxcv/regex that referenced this issue Dec 24, 2014
They're leftovers from the enum namespacing transition

Other than removing public reexports, I also had to publicize a type
(compile::InstIdx). It should have always been public but no
error was ever given because of this bug:

rust-lang/rust#6477

I also had to enable documentation for compile::Ist because it was
complaining that there wasn't any.
@nham
Copy link
Contributor

nham commented Apr 27, 2015

This is still an issue as of:

rustc 1.1.0-nightly (da623844a 2015-04-25) (built 2015-04-26)

The error with glob imports is:

test_6477.rs:8:10: 8:13 error: use of undeclared type name `Foo`
test_6477.rs:8      x : Foo
                        ^~~
error: aborting due to previous error

Without is:

test_6477.rs:6:8: 6:16 error: type `Foo` is private
test_6477.rs:6    use foo::Foo;
                      ^~~~~~~~
error: aborting due to previous error

@pmarcelll
Copy link
Contributor

I seems to be fixed:

error[E0412]: type name `Foo` is undefined or not in scope
 --> <anon>:8:10
  |
8 |      x : Foo
  |          ^^^ undefined or not in scope
  |
  = help: you can import it into scope: `use foo::Foo;`.

error: aborting due to previous error

@Mark-Simulacrum
Copy link
Member

This is still not fixed, and indeed we now suggest to import Foo into scope -- even though this is impossible!

@petrochenkov petrochenkov added the A-diagnostics Area: Messages for errors, warnings, and lints label May 13, 2017
@Mark-Simulacrum
Copy link
Member

E-needstest.

error[E0603]: struct `Foo` is private
 --> test.rs:6:8
  |
6 |    use foo::Foo;
  |        ^^^^^^^^

error: aborting due to previous error(s)

@Mark-Simulacrum Mark-Simulacrum added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. labels Jun 22, 2017
@Mark-Simulacrum
Copy link
Member

gar, no, that was the wrong code sample..

@steveklabnik
Copy link
Member

Today:

error[E0412]: cannot find type `Foo` in this scope
 --> .\foo.rs:8:10
  |
8 |      x : Foo
  |          ^^^ not found in this scope
help: possible candidate is found in another module, you can import it into scope
  |
6 |    use foo::Foo;
  |

Closing!

flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 20, 2020
…s, r=ebroto

Adapted the website search for better matching

* This adds the ability to search for ids with dashes and spaces in the name.
    * Example: `missing-errors-doc` and `missing errors doc` are now valid aliases for lint names
* It also improves the fuzzy search in the description. This search will now match any lint that where all searched words are inside the description.
    * Example: `doc section` finds two lints in our selection

This was suggested/discussed on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Enable.20lint.20search.20with.20dashes/near/220469464)

### Testing
These changes can be tested locally by:
1. Clone this branch
2. Download the current lint index from the [gh-pages branch](https://github.com/rust-lang/rust-clippy/blob/gh-pages/master/lints.json)
3. Put it next to the `util/gh-pages/index.html` and open the html file. Make sure that it can load the lint data. (Browsers can be a bit iffy when opening a loacl html page and loading data)

### Note
I found that searching only a few characters (< 3) seams slow and deleting one even more as almost every lint description contains them. This also happens in our current [lint list](https://rust-lang.github.io/rust-clippy/master/index.html). We could change the search to only be triggered if the search field contains more than 3 letters to slightly improve performance.

---

changelog: Adapted the website search for better matching
flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 2, 2021
…killercup

Website issue tracker link and better search performance

This PR implements some improvements to the website:
1. Added a "Search on Github" link to the "Known problems" section (Closes rust-lang#5386)
    ![example_3](https://user-images.githubusercontent.com/17087237/102718215-e9f12500-42de-11eb-8c1b-487f8184aaf7.png)
    <details>
    <summary>Another mock up I created with the GitHub logo</summary>

    ![example_2](https://user-images.githubusercontent.com/17087237/102718281-3472a180-42df-11eb-99b8-7f6d76da2b55.png)

    </details>

2. Only starting the search after three letters and improving the search performance in general. (Followup rust-lang#6477)

### Testing
These changes can be tested locally by:
1. Clone this branch
2. Download the current lint index from the [gh-pages branch](https://github.com/rust-lang/rust-clippy/blob/gh-pages/master/lints.json)
3. Put it next to the `util/gh-pages/index.html` and open the html file. Make sure that it can load the lint data. (Browsers can be a bit iffy when opening a local html page and loading data)

### Sources for search performance:
1. [A stackoverflow about angular filter performance](https://stackoverflow.com/questions/26876514/optimize-angular-filter-performance)
    * I selected a search debounce of 50ms that's fast enough to catch fast deletion and typing but responsive enough to not bother the user
2. [A stackoverflow about string comparison speeds](https://stackoverflow.com/questions/5296268/fastest-way-to-check-a-string-contain-another-substring-in-javascript)
3. [JS benchmarks for string search performance (`indexOf` seams to be the best)](https://jsben.ch/9cwLJ)

Note: The performance is still a bit poor when going from a specific lint to no search filter. I suspect that angular is recreating all lint items when the filter is cleared causing a major lag spike. The filter functions is at least optimized for little to no search.

---

changelog: Added a "Search on GitHub" link to the website
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

9 participants