summary refs log tree commit diff
path: root/compiler/rustc_infer/src
AgeCommit message (Collapse)AuthorLines
2022-05-14Auto merge of #96883 - jackh726:early-binder-2, r=oli-obkbors-13/+15
Add EarlyBinder Chalk has no concept of `Param` (https://github.com/rust-lang/chalk/blob/e0ade19d139bc784384acc6736cd960c91dd55a1/chalk-ir/src/lib.rs#L579) or `ReEarlyBound` (https://github.com/rust-lang/chalk/blob/e0ade19d139bc784384acc6736cd960c91dd55a1/chalk-ir/src/lib.rs#L1308). Everything is just "bound" - the equivalent of rustc's late-bound. It's not completely clear yet whether to move everything to the same time of binder in rustc or add `Param` and `ReEarlyBound` in Chalk. Either way, tracking when we have or haven't already substituted out these in rustc can be helpful. As a first step, I'm just adding a `EarlyBinder` newtype that is required to call `subst`. I also add a couple "transparent" `bound_*` wrappers around a couple query that are often immediately substituted. r? `@nikomatsakis`
2022-05-14Add bound_explicit_item_bounds and bound_item_boundsJack Huey-7/+7
2022-05-13Add bound_fn_sigJack Huey-4/+4
2022-05-13Add bound_type_ofJack Huey-1/+1
2022-05-11stop suggesting non-existing fully qualified pathsTakayuki Maeda-11/+16
2022-05-10Introduce EarlyBinderJack Huey-12/+14
2022-05-08Auto merge of #96155 - jackh726:param-heuristics-followup, r=estebankbors-3/+4
Followups for method call error change Each commit is self-contained. Fixes most of the followup reviews from that PR. r? `@estebank`
2022-05-07Auto merge of #96094 - Elliot-Roberts:fix_doctests, r=compiler-errorsbors-45/+54
Begin fixing all the broken doctests in `compiler/` Begins to fix #95994. All of them pass now but 24 of them I've marked with `ignore HELP (<explanation>)` (asking for help) as I'm unsure how to get them to work / if we should leave them as they are. There are also a few that I marked `ignore` that could maybe be made to work but seem less important. Each `ignore` has a rough "reason" for ignoring after it parentheses, with - `(pseudo-rust)` meaning "mostly rust-like but contains foreign syntax" - `(illustrative)` a somewhat catchall for either a fragment of rust that doesn't stand on its own (like a lone type), or abbreviated rust with ellipses and undeclared types that would get too cluttered if made compile-worthy. - `(not-rust)` stuff that isn't rust but benefits from the syntax highlighting, like MIR. - `(internal)` uses `rustc_*` code which would be difficult to make work with the testing setup. Those reason notes are a bit inconsistently applied and messy though. If that's important I can go through them again and try a more principled approach. When I run `rg '```ignore \(' .` on the repo, there look to be lots of different conventions other people have used for this sort of thing. I could try unifying them all if that would be helpful. I'm not sure if there was a better existing way to do this but I wrote my own script to help me run all the doctests and wade through the output. If that would be useful to anyone else, I put it here: https://github.com/Elliot-Roberts/rust_doctest_fixing_tool
2022-05-06Resolve vars in note_type_errJack Huey-3/+4
2022-05-06suggest fully qualified path with appropriate paramsTakayuki Maeda-1/+22
2022-05-04Stabilize `bool::then_some`Josh Triplett-1/+0
2022-05-02fix most compiler/ doctestsElliot Roberts-45/+54
2022-05-02rustc: Panic by default in `DefIdTree::parent`Vadim Petrochenkov-6/+4
Only crate root def-ids don't have a parent, and in majority of cases the argument of `DefIdTree::parent` cannot be a crate root. So we now panic by default in `parent` and introduce a new non-panicing function `opt_parent` for cases where the argument can be a crate root. Same applies to `local_parent`/`opt_local_parent`.
2022-04-30Auto merge of #96347 - estebank:issue-96292, r=compiler-errorsbors-10/+31
Erase type params when suggesting fully qualified path When suggesting the use of a fully qualified path for a method call that is ambiguous because it has multiple candidates, erase type params in the resulting code, as they would result in an error when applied. We replace them with `_` in the output to rely on inference. There might be cases where this still produces slighlty incomplete suggestions, but it otherwise produces many more errors in relatively common cases. Fix #96292
2022-04-30Store all generic bounds as where predicates.Camille GILLOT-44/+23
2022-04-30Inline WhereClause into Generics.Camille GILLOT-4/+4
2022-04-29errors: `span_suggestion` takes `impl ToString`David Wood-1/+1
Change `span_suggestion` (and variants) to take `impl ToString` rather than `String` for the suggested code, as this simplifies the requirements on the diagnostic derive. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-28Rollup merge of #96409 - ↵Dylan DPC-75/+77
marmeladema:fix-nll-introduce-named-lifetime-suggestion, r=jackh726 Recover suggestions to introduce named lifetime under NLL Fixes #96157 r? ```@jackh726``` Built on top of #96385 so only the second commit is relevant
2022-04-28Rollup merge of #96471 - BoxyUwU:let_else_considered_harmful, r=lcnrDylan DPC-14/+9
replace let else with `?` r? `@oli-obk`
2022-04-27Recover suggestions to introduce named lifetime under NLLmarmeladema-75/+77
2022-04-27tut tut tutEllen-14/+9
2022-04-25Recover most `impl Trait` and `dyn Trait` lifetime bound suggestions under NLLmarmeladema-65/+74
2022-04-24Improve span for `consider adding an explicit lifetime bound` suggestions ↵marmeladema-28/+25
under NLL Because NLL borrowck is run after typeck, `in_progress_typeck_results` was always `None` which was preventing the retrieval of the span to which the suggestion is suppose to add the lifetime bound. We now manually pass the `LocalDefId` owner to `construct_generic_bound_failure` so that under NLL, we give the owner id of the current body.
2022-04-23Provide consistent output order for suggestionsEsteban Küber-3/+4
2022-04-23Use more targetted suggestion span for fully qualified pathEsteban Küber-7/+10
2022-04-23Erase type params when suggesting fully qualified pathEsteban Küber-0/+17
When suggesting the use of a fully qualified path for a method call that is ambiguous because it has multiple candidates, erase type params in the resulting code, as they would result in an error when applied. We replace them with `_` in the output to rely on inference. There might be cases where this still produces slighlty incomplete suggestions, but it otherwise produces many more errors in relatively common cases. Fix #96292
2022-04-19Auto merge of #96020 - martingms:optimize-relate_substs, r=nnethercotebors-3/+9
Micro-optimize `ty::relate::relate_substs` by avoiding `match` Was a top-20 hot function in a callgrind profile of compiling `bitmaps-3.1.0`. Yields some small speedups on that crate and some others according to local benching: Benchmark | Profile | Scenario | % Change | Significance Factor? -- | -- | -- | -- | -- bitmaps-3.1.0 | check | full | -1.88% | 9.42x bitmaps-3.1.0 | debug | full | -1.80% | 8.99x bitmaps-3.1.0 | opt | full | -1.70% | 8.49x bitmaps-3.1.0 | check | incr-full | -1.54% | 7.68x deep-vector | debug | full | 1.52% | 7.61x bitmaps-3.1.0 | debug | incr-full | -1.45% | 7.26x bitmaps-3.1.0 | opt | incr-full | -1.39% | 6.95x nalgebra-0.30.1 | check | full | -0.68% | 3.42x nalgebra-0.30.1 | debug | full | -0.64% | 3.22x nalgebra-0.30.1 | opt | full | -0.64% | 3.20x projection-caching | check | full | -0.61% | 3.05x nalgebra-0.30.1 | check | incr-full | -0.56% | 2.78x nalgebra-0.30.1 | opt | incr-full | -0.54% | 2.72x nalgebra-0.30.1 | debug | incr-full | -0.54% | 2.69x projection-caching | check | incr-full | -0.50% | 2.51x tt-muncher | opt | full | -0.48% | 2.42x projection-caching | opt | full | -0.47% | 2.37x projection-caching | debug | full | -0.47% | 2.35x projection-caching | opt | incr-full | -0.44% | 2.21x projection-caching | debug | incr-full | -0.42% | 2.08x deeply-nested-multi | check | incr-full | 0.37% | 1.87x wf-projection-stress-65510 | opt | full | -0.37% | 1.84x deep-vector | debug | incr-patched: add vec item | -0.32% | 1.61x projection-caching | debug | incr-unchanged | -0.32% | 1.60x wf-projection-stress-65510 | check | full | -0.31% | 1.55x projection-caching | opt | incr-unchanged | -0.31% | 1.53x wf-projection-stress-65510 | debug | incr-full | -0.30% | 1.51x wf-projection-stress-65510 | opt | incr-full | -0.30% | 1.51x r? `@nnethercote`
2022-04-17Split relate_substs into two functionsMartin Gammelsæter-3/+9
One for the case with variances, and one without. All callers use an explicit Option for the variable anyway.
2022-04-16Rollup merge of #95908 - compiler-errors:shallow_resolve_ty-inline, r=oli-obkDylan DPC-44/+39
Inline `shallow_resolve_ty` into `ShallowResolver` addresses fixme I found in infcx
2022-04-16Auto merge of #92364 - jackh726:Quantumplation/65853/param-heuristics, ↵bors-6/+7
r=estebank Better method call error messages Rebase/continuation of #71827 ~Based on #92360~ ~Based on #93118~ There's a decent description in #71827 that I won't copy here (for now at least) In addition to rebasing, I've tried to restore most of the original suggestions for invalid arguments. Unfortunately, this does make some of the errors a bit verbose. To fix this will require a bit of refactoring to some of the generalized error suggestion functions, and I just don't have the time to go into it right now. I think this is in a state that the error messages are overall better than before without a reduction in the suggestions given. ~I've tried to split out some of the easier and self-contained changes into separate commits (mostly in #92360, but also one here). There might be more than can be done here, but again just lacking time.~ r? `@estebank` as the original reviewer of #71827
2022-04-16Implementation for 65853Jack Huey-6/+7
This attempts to bring better error messages to invalid method calls, by applying some heuristics to identify common mistakes. The algorithm is inspired by Levenshtein distance and longest common sub-sequence. In essence, we treat the types of the function, and the types of the arguments you provided as two "words" and compute the edits to get from one to the other. We then modify that algorithm to detect 4 cases: - A function input is missing - An extra argument was provided - The type of an argument is straight up invalid - Two arguments have been swapped - A subset of the arguments have been shuffled (We detect the last two as separate cases so that we can detect two swaps, instead of 4 parameters permuted.) It helps to understand this argument by paying special attention to terminology: "inputs" refers to the inputs being *expected* by the function, and "arguments" refers to what has been provided at the call site. The basic sketch of the algorithm is as follows: - Construct a boolean grid, with a row for each argument, and a column for each input. The cell [i, j] is true if the i'th argument could satisfy the j'th input. - If we find an argument that could satisfy no inputs, provided for an input that can't be satisfied by any other argument, we consider this an "invalid type". - Extra arguments are those that can't satisfy any input, provided for an input that *could* be satisfied by another argument. - Missing inputs are inputs that can't be satisfied by any argument, where the provided argument could satisfy another input - Swapped / Permuted arguments are identified with a cycle detection algorithm. As each issue is found, we remove the relevant inputs / arguments and check for more issues. If we find no issues, we match up any "valid" arguments, and start again. Note that there's a lot of extra complexity: - We try to stay efficient on the happy path, only computing the diagonal until we find a problem, and then filling in the rest of the matrix. - Closure arguments are wrapped in a tuple and need to be unwrapped - We need to resolve closure types after the rest, to allow the most specific type constraints - We need to handle imported C functions that might be variadic in their inputs. I tried to document a lot of this in comments in the code and keep the naming clear.
2022-04-15Rollup merge of #96026 - matthiaskrgr:clippy_compl_1304, r=Dylan-DPCDylan DPC-3/+3
couple of clippy::complexity fixes
2022-04-15Rollup merge of #94457 - jhpratt:stabilize-derive_default_enum, r=davidtwcoDylan DPC-1/+1
Stabilize `derive_default_enum` This stabilizes `#![feature(derive_default_enum)]`, as proposed in [RFC 3107](https://github.com/rust-lang/rfcs/pull/3107) and tracked in #87517. In short, it permits you to `#[derive(Default)]` on `enum`s, indicating what the default should be by placing a `#[default]` attribute on the desired variant (which must be a unit variant in the interest of forward compatibility). ```````@rustbot``````` label +S-waiting-on-review +T-lang
2022-04-13couple of clippy::complexity fixesMatthias Krüger-3/+3
2022-04-10Inline shallow_resolve_ty into ShallowResolverMichael Goulet-44/+39
2022-04-09Auto merge of #95524 - oli-obk:cached_stable_hash_cleanups, r=nnethercotebors-18/+17
Cached stable hash cleanups r? `@nnethercote` Add a sanity assertion in debug mode to check that the cached hashes are actually the ones we get if we compute the hash each time. Add a new data structure that bundles all the hash-caching work to make it easier to re-use it for different interned data structures
2022-04-08Avoid looking at the internals of Interned directlyOli Scherer-18/+17
2022-04-07Stabilize `derive_default_enum`Jacob Pratt-1/+1
2022-04-05errors: implement fallback diagnostic translationDavid Wood-4/+3
This commit updates the signatures of all diagnostic functions to accept types that can be converted into a `DiagnosticMessage`. This enables existing diagnostic calls to continue to work as before and Fluent identifiers to be provided. The `SessionDiagnostic` derive just generates normal diagnostic calls, so these APIs had to be modified to accept Fluent identifiers. In addition, loading of the "fallback" Fluent bundle, which contains the built-in English messages, has been implemented. Each diagnostic now has "arguments" which correspond to variables in the Fluent messages (necessary to render a Fluent message) but no API for adding arguments has been added yet. Therefore, diagnostics (that do not require interpolation) can be converted to use Fluent identifiers and will be output as before.
2022-04-05span: move `MultiSpan`David Wood-11/+9
`MultiSpan` contains labels, which are more complicated with the introduction of diagnostic translation and will use types from `rustc_errors` - however, `rustc_errors` depends on `rustc_span` so `rustc_span` cannot use types like `DiagnosticMessage` without dependency cycles. Introduce a new `rustc_error_messages` crate that can contain `DiagnosticMessage` and `MultiSpan`. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-02Rollup merge of #95560 - lcnr:obligation-cause, r=oli-obkDylan DPC-13/+17
convert more `DefId`s to `LocalDefId`
2022-04-02Rollup merge of #95559 - lcnr:inferctxt-typeck, r=oli-obkDylan DPC-20/+10
small type system refactoring
2022-04-01convert more `DefId`s to `LocalDefId`lcnr-13/+17
2022-04-01remove `unify_key::replace_if_possible`lcnr-16/+7
2022-04-01update commentslcnr-4/+3
2022-04-01Rollup merge of #95260 - compiler-errors:fn, r=davidtwcoMatthias Krüger-1/+5
Better suggestions for `Fn`-family trait selection errors 1. Suppress suggestions to add `std::ops::Fn{,Mut,Once}` bounds when a type already implements `Fn{,Mut,Once}` 2. Add a note that points out that a type does in fact implement `Fn{,Mut,Once}`, but the arguments vary (either by number or by actual arguments) 3. Add a note that points out that a type does in fact implement `Fn{,Mut,Once}`, but not the right one (e.g. implements `FnMut`, but `Fn` is required). Fixes #95147
2022-03-31remove unused field from `infcx`lcnr-26/+2
2022-03-30Addressed comments by @compiler-errors and @bjorn3Yuri Astrakhan-2/+2
2022-03-30Spellchecking compiler commentsYuri Astrakhan-16/+16
This PR cleans up the rest of the spelling mistakes in the compiler comments. This PR does not change any literal or code spelling issues.
2022-03-30Auto merge of #94081 - oli-obk:lazy_tait_take_two, r=nikomatsakisbors-307/+603
Lazy type-alias-impl-trait take two ### user visible change 1: RPIT inference from recursive call sites Lazy TAIT has an insta-stable change. The following snippet now compiles, because opaque types can now have their hidden type set from wherever the opaque type is mentioned. ```rust fn bar(b: bool) -> impl std::fmt::Debug { if b { return 42 } let x: u32 = bar(false); // this errors on stable 99 } ``` The return type of `bar` stays opaque, you can't do `bar(false) + 42`, you need to actually mention the hidden type. ### user visible change 2: divergence between RPIT and TAIT in return statements Note that `return` statements and the trailing return expression are special with RPIT (but not TAIT). So ```rust #![feature(type_alias_impl_trait)] type Foo = impl std::fmt::Debug; fn foo(b: bool) -> Foo { if b { return vec![42]; } std::iter::empty().collect() //~ ERROR `Foo` cannot be built from an iterator } fn bar(b: bool) -> impl std::fmt::Debug { if b { return vec![42] } std::iter::empty().collect() // Works, magic (accidentally stabilized, not intended) } ``` But when we are working with the return value of a recursive call, the behavior of RPIT and TAIT is the same: ```rust type Foo = impl std::fmt::Debug; fn foo(b: bool) -> Foo { if b { return vec![]; } let mut x = foo(false); x = std::iter::empty().collect(); //~ ERROR `Foo` cannot be built from an iterator vec![] } fn bar(b: bool) -> impl std::fmt::Debug { if b { return vec![]; } let mut x = bar(false); x = std::iter::empty().collect(); //~ ERROR `impl Debug` cannot be built from an iterator vec![] } ``` ### user visible change 3: TAIT does not merge types across branches In contrast to RPIT, TAIT does not merge types across branches, so the following does not compile. ```rust type Foo = impl std::fmt::Debug; fn foo(b: bool) -> Foo { if b { vec![42_i32] } else { std::iter::empty().collect() //~^ ERROR `Foo` cannot be built from an iterator over elements of type `_` } } ``` It is easy to support, but we should make an explicit decision to include the additional complexity in the implementation (it's not much, see a721052457cf513487fb4266e3ade65c29b272d2 which needs to be reverted to enable this). ### PR formalities previous attempt: #92007 This PR also includes #92306 and #93783, as they were reverted along with #92007 in #93893 fixes #93411 fixes #88236 fixes #89312 fixes #87340 fixes #86800 fixes #86719 fixes #84073 fixes #83919 fixes #82139 fixes #77987 fixes #74282 fixes #67830 fixes #62742 fixes #54895