resolve: Simplify treatment of ambiguity errors
If we have a glob conflict like this
```rust
mod m1 { struct S; }
mod m2 { struct S; }
use m1::*;
use m2::*;
```
we treat it as a special "ambiguity item" that's not an error by itself, but produces an error when actually used.
```rust
use m1::*; // primary
use m2::*; // secondary
=>
ambiguity S(m1::S, m2::S);
```
Ambiguity items were *sometimes* treated as their primary items for error recovery, but pretty irregularly.
After this PR they are always treated as their primary items, except that
- If an ambiguity item is marked as used, then it still produces an error.
- Ambiguity items are still filtered away when exported to other crates (which is also a use in some sense).
privacy: Use common `DefId` visiting infrastructure for all privacy visitors
One repeating pattern in privacy checking is going through a type, visiting all `DefId`s inside it and doing something with them.
This is the case because visibilities and reachabilities are attached to `DefId`s.
Previously various privacy visitors visited types slightly differently using their own methods, with most recently written `TypePrivacyVisitor` being the "gold standard".
This mostly worked okay, but differences could manifest in overly conservative reachability analysis, some errors being reported twice, some private-in-public lints (not errors) being wrongly reported or not reported.
This PR does something that I wanted to do since https://github.com/rust-lang/rust/pull/32674#discussion_r58291608 - factoring out the common visiting logic!
Now all the common logic is contained in `struct DefIdVisitorSkeleton`, with specific privacy visitors deciding only what to do with visited `DefId`s (via `trait DefIdVisitor`).
A bunch of cleanups is also applied in the process.
This area is somewhat tricky due to lots of easily miss-able details, but thankfully it's was well covered by tests in https://github.com/rust-lang/rust/pull/46083 and previous PRs, so I'm relatively sure in the refactoring correctness.
Fixes https://github.com/rust-lang/rust/pull/56837#discussion_r241962239 in particular.
Also this will help with implementing https://github.com/rust-lang/rust/issues/48054.
Add `-Z instrument-mcount`
This flag inserts `mcount` function call to the beginning of every function
after inline processing. So tracing tools like uftrace [1] (or ftrace for
Linux kernel modules) have a chance to examine function calls.
It is similar to the `-pg` flag provided by gcc or clang, but without
generating a `__gmon_start__` function for executables. If a program
runs without being traced, no `gmon.out` will be written to disk.
Under the hood, it simply adds `"instrument-function-entry-inlined"="mcount"`
attribute to every function. The `post-inline-ee-instrument` LLVM pass does
the actual job.
[1]: https://github.com/namhyung/uftrace
This flag inserts `mcount` function call to the beginning of every function
after inline processing. So tracing tools like uftrace [1] (or ftrace for
Linux kernel modules) have a chance to examine function calls.
It is similar to the `-pg` flag provided by gcc or clang, but without
generating a `__gmon_start__` function for executables. If a program
runs without being traced, no `gmon.out` will be written to disk.
Under the hood, it simply adds `"instrument-function-entry-inlined"="mcount"`
attribute to every function. The `post-inline-ee-instrument` LLVM pass does
the actual job.
[1]: https://github.com/namhyung/uftrace
submodules: update clippy from f7bdf500 to 39bd8449
Fixes clippy toolstate
Changes:
````
UI test cleanup: Extract iter_skip_next from methods.rs
Update test output after rebase
Remove false negatives from known problems
Implement use_self for tuple structs
Document known problems
rustup https://github.com/rust-lang/rust/pull/56225/
Remove unnecessary `use` statements after `cargo fix`
Apply cargo fix --edition-idioms fixes
Use match ergonomics for booleans lint
Use match ergonomics for block_in_if_condition lint
Use match ergonomics for bit_mask lint
Use match ergonomics for attrs lint
Use match ergonomics for assign_ops lint
Use match ergonomics for artithmetic lint
Use match ergonomics for approx_const lint
Remove crate:: prefixes from crate paths
Support array indexing expressions in unused write to a constant
Mark writes to constants as side-effect-less
Update README local run command to remove syspath
Remove unsafe from consts clippy lints
Fix formatting
Merge new_without_default_derive into new_without_default
Only print out question_mark lint when it actually triggered
Add failing test
Reinserted commata
Recomend `.as_ref()?` in certain situations
Deduplicate some code?
````
r? @oli-obk or anyone else
Changes:
````
UI test cleanup: Extract iter_skip_next from methods.rs
Update test output after rebase
Remove false negatives from known problems
Implement use_self for tuple structs
Document known problems
rustup https://github.com/rust-lang/rust/pull/56225/
Remove unnecessary `use` statements after `cargo fix`
Apply cargo fix --edition-idioms fixes
Use match ergonomics for booleans lint
Use match ergonomics for block_in_if_condition lint
Use match ergonomics for bit_mask lint
Use match ergonomics for attrs lint
Use match ergonomics for assign_ops lint
Use match ergonomics for artithmetic lint
Use match ergonomics for approx_const lint
Remove crate:: prefixes from crate paths
Support array indexing expressions in unused write to a constant
Mark writes to constants as side-effect-less
Update README local run command to remove syspath
Remove unsafe from consts clippy lints
Fix formatting
Merge new_without_default_derive into new_without_default
Only print out question_mark lint when it actually triggered
Add failing test
Reinserted commata
Recomend `.as_ref()?` in certain situations
Deduplicate some code?
````
Mention ToString in std::fmt docs
I believe this should be added because `x.to_string()` is preferred over `format!("{}", x)` as the recommended way to convert a value to a string. `std::fmt` and the `format` macro is where people look for documentation about this, and thus we should include references to this preferred functions there.
Resolves#55065
resolve: Fix one more ICE in import validation
So if you have an unresolved import
```rust
mod m {
use foo::bar;
}
```
error recovery will insert a special item with `Def::Err` definition into module `m`, so other things depending on `bar` won't produce extra errors.
The issue was that erroneous `bar` was overwriting legitimate `bar`s coming from globs, e.g.
```rust
mod m {
use baz::*; // imports real existing `bar`
use foo::bar;
}
```
causing some unwanted diagnostics talking about "unresolved items", and producing inconsistent resolutions like https://github.com/rust-lang/rust/issues/57015.
This PR stops overwriting real successful resolutions with `Def::Err`s.
Fixes https://github.com/rust-lang/rust/issues/57015
Implement RFC 2338, "Type alias enum variants"
This PR implements [RFC 2338](https://github.com/rust-lang/rfcs/pull/2338), allowing one to write code like the following.
```rust
#![feature(type_alias_enum_variants)]
enum Foo {
Bar(i32),
Baz { i: i32 },
}
type Alias = Foo;
fn main() {
let t = Alias::Bar(0);
let t = Alias::Baz { i: 0 };
match t {
Alias::Bar(_i) => {}
Alias::Baz { i: _i } => {}
}
}
```
Since `Self` can be considered a type alias in this context, it also enables using `Self::Variant` as both a constructor and pattern.
Fixes issues #56199 and #56611.
N.B., after discussing the syntax for type arguments on enum variants with @petrochenkov and @eddyb (there are also a few comments on the [tracking issue](https://github.com/rust-lang/rust/issues/49683)), the consensus seems to be treat the syntax as follows, which ought to be backwards-compatible.
```rust
Option::<u8>::None; // OK
Option::None::<u8>; // OK, but lint in near future (hard error next edition?)
Alias::<u8>::None; // OK
Alias::None::<u8>; // Error
```
I do not know if this will need an FCP, but let's start one if so.
resolve: Fix another ICE in import validation
Imports are allowed to have ambiguous resolutions as long as all of them have same `Def`.
As it turned out, it's possible for different `Module`s to have same `Def` when `extern crate` items are involved.
Fixes https://github.com/rust-lang/rust/issues/56596