Commit graph

372 commits

Author SHA1 Message Date
Tyler Mandry
6af831ace6 Add tracing instrumentation to method typeck
I was recently digging into how this code works, and this
instrumentation was helpful.
2021-01-14 01:38:02 +00:00
Mara Bos
9e8edc8c22
Rollup merge of #80495 - jyn514:rename-empty, r=petrochenkov
Rename kw::Invalid -> kw::Empty

See https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Is.20there.20a.20symbol.20for.20the.20empty.20string.3F/near/220054471
for context.

r? `@petrochenkov`
2020-12-30 20:56:58 +00:00
Joshua Nelson
edeac1778c Rename kw::Invalid -> kw::Empty
See https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Is.20there.20a.20symbol.20for.20the.20empty.20string.3F/near/220054471
for context.
2020-12-30 09:50:02 -05:00
Yuki Okushi
07083739fb
Rollup merge of #80492 - matthiaskrgr:tasty_wraps, r=varkor
remove empty wraps, don't return Results from from infallible functions

This makes code easier to understand because it is more obvious when a function actually can't fail (return Err or None)

Make functions that only ever return Some(x), return x directly
Remove return type from functions that return Option<(), Err> but would only ever return Ok(()).

Found with `clippy::unnecessary_wraps`
2020-12-30 22:49:24 +09:00
Matthias Krüger
e5ead5fc58 remove unused return types such as empty Results or Options that would always be Some(..)
remove unused return type of dropck::check_drop_obligations()
don't wrap return type in Option in get_macro_by_def_id() since we would always return Some(..)
remove redundant return type of back::write::optimize()
don't Option-wrap return type of compute_type_parameters() since we always return Some(..)
don't return empty Result in assemble_generator_candidates()
don't return empty Result in assemble_closure_candidates()
don't return empty result in assemble_fn_pointer_candidates()
don't return empty result in assemble_candidates_from_impls()
don't return empty result in assemble_candidates_from_auto_impls()
don't return emtpy result in assemble_candidates_for_trait_alias()
don't return empty result in assemble_builtin_bound_candidates()
don't return empty results in assemble_extension_candidates_for_traits_in_scope() and assemble_extension_candidates_for_trait()
remove redundant wrapping of return type of StripItem::strip() since it always returns Some(..)
remove unused return type of assemble_extension_candidates_for_all_traits()
2020-12-30 13:15:40 +01:00
Yuki Okushi
3812909919
Rollup merge of #80487 - matthiaskrgr:field_names, r=jyn514
don't redundantly repeat field names

r? `@Dylan-DPC`
2020-12-30 18:15:29 +09:00
Yuki Okushi
9576ee97d1
Rollup merge of #80477 - tmiasko:safe-forget, r=oli-obk
Make forget intrinsic safe
2020-12-30 18:15:25 +09:00
Matthias Krüger
0c3af22e08 don't redundantly repeat field names 2020-12-29 22:26:58 +01:00
Vadim Petrochenkov
4d2d0bad4e Remove compile-fail test suite 2020-12-29 23:39:56 +03:00
Tomasz Miąsko
5718cc2f9b Make forget intrinsic safe 2020-12-29 00:00:00 +00:00
Dylan DPC
c51172f38a
Rollup merge of #80344 - matthiaskrgr:matches, r=Dylan-DPC
use matches!() macro in more places
2020-12-28 14:13:12 +01:00
Dylan DPC
12ac312351
Rollup merge of #80284 - ThePuzzlemaker:issue-80179-fix, r=varkor
Suggest fn ptr rather than fn item and suggest to use `Fn` trait bounds rather than the unique closure type in E0121

Previously, using `_` as a return type in a function that returned a function/closure would provide a diagnostic that would cause a papercut. For example:
```rust
fn f() -> i32 { 0 }
fn fn_ptr() -> _ { f }
fn closure() -> _ { || 0 }
```
would result in this diagnostic:
```rust
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> <anon>:2:16
  |
2 | fn fn_ptr() -> _ { f }
  |                ^
  |                |
  |                not allowed in type signatures
  |                help: replace with the correct return type: `fn() -> i32 {f}`

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> <anon>:3:17
  |
3 | fn closure() -> _ { || 0 }
  |                 ^
  |                 |
  |                 not allowed in type signatures
  |                 help: replace with the correct return type: `[closure@<anon>:3:21: 3:25]`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0121`.
```
As can be seen, it was suggested to use the function definition return type `fn() -> i32 { f }` which is not valid syntax as a return type. Additionally, closures cause a papercut as unique closure types (notated in this case as `[closure@<anon>:3:21: 3:25]`) are not valid syntax either.

Instead, this PR implements this version of the diagnostic (this example is for the same code featured above):
```rust
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> <anon>:2:16
  |
2 | fn fn_ptr() -> _ { f }
  |                ^
  |                |
  |                not allowed in type signatures
  |                help: replace with the correct return type: `fn() -> i32`

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> <anon>:3:17
  |
3 | fn closure() -> _ { || 0 }
  |                 ^ not allowed in type signatures
  |
  = help: consider using an `Fn`, `FnMut`, or `FnOnce` trait bound
  = note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0121`.
```
As can be seen in this diagnostic, the papercut for returning a function item is fixed by suggesting the usage of a function pointer as the return type. As for closures, it's suggested to use an `Fn`, `FnMut`, or `FnOnce` trait bound (with further reading on closures and `Fn` traits in *The Book* for beginners). I did not implement a suggestion to use `impl Fn() -> i32` syntax as that was out-of-scope for my abilities at the moment, therefore someone in the future may want to implement that. Also, it's possible to use either `impl Trait` syntax, generics, or generics with a `where` clause, and some users may not want to use `impl Trait` syntax for their own reasons.

This PR fixes #80179.
2020-12-28 14:13:08 +01:00
Bastian Kauschke
06cc9c26da stabilize min_const_generics 2020-12-26 18:24:10 +01:00
Matthias Krüger
d12a358673 use matches!() macro in more places 2020-12-24 13:35:12 +01:00
ThePuzzlemaker
4f6be466fd
Suggest fn ptr rather than fn item and suggest to use Fn trait bounds rather than the unique closure type in E0121
This is a squash of the titular commit along with these minor commits:
- Improve note
- Improve note pt2
2020-12-22 13:26:28 -06:00
LingMan
ef75761fb1 Turn helper method into a closure
`replace_prefix` is currently implemented as a method but has no real relation
to the struct it is implemented on. Turn it into a closure and move it into the
only method from which it is called.
2020-12-22 16:18:34 +01:00
bors
9310aff66c Auto merge of #80208 - bugadani:generics-of-alloc, r=matthewjasper
Reserve necessary space for params in generics_of

Always reserve space for the exact number of generic parameters we need in generics_of. As far as I can see, the default is 0/4 elements based on has_self, and the vector grows on after that.
2020-12-22 00:20:14 +00:00
Dylan DPC
d729e76492
Rollup merge of #80170 - ldm0:fixice, r=lcnr
Fix ICE when lookup method in trait for type that have bound vars

Closes #77910
2020-12-21 02:47:39 +01:00
bors
c609b2eaf3 Auto merge of #78317 - est31:linear_in_impl_count, r=matthewjasper
Turn quadratic time on number of impl blocks into linear time

Previously, if you had a lot of inherent impl blocks on a type like:

```Rust
struct Foo;

impl Foo { fn foo_1() {} }
// ...
impl Foo { fn foo_100_000() {} }
```

The compiler would be very slow at processing it, because
an internal algorithm would run in O(n^2), where n is the number
of impl blocks. Now, we add a new algorithm that allocates but
is faster asymptotically.

Comparing rustc nightly with a local build of rustc as of this PR (results in seconds):

| N | real time before | real time after |
| - | - | - |
| 4_000 | 0.57 | 0.46 |
| 8_000  | 1.31  | 0.84 |
| 16_000  | 3.56 | 1.69 |
| 32_000 | 10.60 | 3.73 |

I've tuned up the numbers to make the effect larger than the startup noise of rustc, but the asymptotic difference should hold for smaller n as well.

Note: current state of the PR omits error messages if there are other errors present already. For now, I'm mainly interested in a perf run to study whether this issue is present at all. Please queue one for this PR. Thanks!
2020-12-20 19:54:15 +00:00
Donough Liu
4eb28c358c
Update compiler/rustc_typeck/src/check/op.rs
Co-authored-by: lcnr <bastian_kauschke@hotmail.de>
2020-12-20 21:45:23 +08:00
Donough Liu
a33f6ac9a0 Fix ICE on suggesting calling function 2020-12-20 19:53:22 +08:00
bors
b1964e60b7 Auto merge of #80163 - jackh726:binder-refactor-part-3, r=lcnr
Make BoundRegion have a kind of BoungRegionKind

Split from #76814

Also includes making `replace_escaping_bound_vars` only return `T`

Going to r? `@lcnr`
Feel free to reassign
2020-12-20 07:01:00 +00:00
Dániel Buga
01d7f87d8e Reserve necessary space for params in generics_of 2020-12-20 00:50:06 +01:00
Jack Huey
5e7095850c More rebinds 2020-12-19 04:26:35 -05:00
Jack Huey
af3b1cb0b5 Change potentially_qualified to be defined on Binder<PredicateAtom> 2020-12-18 15:57:12 -05:00
Jack Huey
328fcee4af Make BoundRegion have a kind of BoungRegionKind 2020-12-18 15:27:28 -05:00
pierwill
605c978641
Fix typo in rustc_typeck docs 2020-12-17 20:47:05 -08:00
bors
eb4fc71dc9 Auto merge of #79945 - jackh726:existential_trait_ref, r=nikomatsakis
Move binder for dyn to each list item

This essentially changes `ty::Binder<&'tcx List<ExistentialTraitRef>>` to `&'tcx List<ty::Binder<ExistentialTraitRef>>`.

This is a first step in moving the `dyn Trait` representation closer to Chalk, which we've talked about in `@rust-lang/wg-traits.`

r? `@nikomatsakis`
2020-12-17 18:21:20 +00:00
bors
9b84d36a0b Auto merge of #80105 - JohnTitor:rollup-8c030u5, r=JohnTitor
Rollup of 11 pull requests

Successful merges:

 - #79051 (Implement if-let match guards)
 - #79877 (Allow `since="TBD"` for rustc_deprecated)
 - #79882 (Fix issue #78496)
 - #80026 (expand-yaml-anchors: Make the output directory separator-insensitive)
 - #80039 (Remove unused `TyEncoder::tcx` required method)
 - #80069 (Test that `core::assert!` is valid)
 - #80072 (Fixed conflict with drop elaboration and coverage)
 - #80073 (Add support for target aliases)
 - #80082 (Revert #78790 - rust-src vendoring)
 - #80097 (Add `popcount` and `popcnt` as doc aliases for `count_ones` methods.)
 - #80103 (Remove docs for non-existent parameters in `rustc_expand`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2020-12-17 03:07:09 +00:00
Yuki Okushi
1e1ba7c936
Rollup merge of #79051 - LeSeulArtichaut:if-let-guard, r=matthewjasper
Implement if-let match guards

Implements rust-lang/rfcs#2294 (tracking issue: #51114).

I probably should do a few more things before this can be merged:
- [x] Add tests (added basic tests, more advanced tests could be done in the future?)
- [x] Add lint for exhaustive if-let guard (comparable to normal if-let statements)
- [x] Fix clippy

However since this is a nightly feature maybe it's fine to land this and do those steps in follow-up PRs.

Thanks a lot `@matthewjasper` ❤️ for helping me with lowering to MIR! Would you be interested in reviewing this?
r? `@ghost` for now
2020-12-17 11:43:55 +09:00
bors
a6491be5be Auto merge of #79790 - LeSeulArtichaut:issue-79683, r=lcnr
Take into account negative impls in "trait item not found" suggestions

This removes the suggestion to implement a trait for a type when that type already has a negative implementation for the trait, and replaces it with a note to point out that the trait is explicitely unimplemented, as suggested by `@scottmcm.`

Helps with #79683.

r? `@scottmcm` do you want to review this?
2020-12-17 00:15:45 +00:00
LeSeulArtichaut
cfc38d2d08 Take into account negative impls in "trait item not found" suggestions 2020-12-16 14:41:47 +01:00
bors
d149b6579f Auto merge of #79956 - camelid:variant-field-vis, r=petrochenkov
Resolve enum field visibility correctly

Fixes #79593. 🎉

Previously, this code treated enum fields' visibility as if they were
struct fields. However, that's not correct because the visibility of a
struct field with `ast::VisibilityKind::Inherited` is private to the
module it's defined in, whereas the visibility of an *enum* field with
`ast::VisibilityKind::Inherited` is the visibility of the enum it
belongs to.
2020-12-13 08:40:07 +00:00
Yuki Okushi
1b81f08d4c
Rollup merge of #79940 - matthiaskrgr:cl15ppy, r=Dylan-DPC
fix more clippy::complexity findings

fix clippy::unnecessary_filter_map
use if let Some(x) = ..  instead of ...map(|x|) to conditionally run fns that return () (clippy::option_map_unit_fn)
fix clippy::{needless_bool, manual_unwrap_or}
don't clone types that are copy (clippy::clone_on_copy)
don't convert types into identical types with .into() (clippy::useless_conversion)
use strip_prefix over slicing (clippy::manual_strip)

r? ``@Dylan-DPC``
2020-12-13 11:05:36 +09:00
Camelid
5ce3f4c166 Resolve enum field visibility correctly
Previously, this code treated enum fields' visibility as if they were
struct fields. However, that's not correct because the visibility of a
struct field with `ast::VisibilityKind::Inherited` is private to the
module it's defined in, whereas the visibility of an *enum* field with
`ast::VisibilityKind::Inherited` is the visibility of the enum it
belongs to.
2020-12-12 14:04:59 -08:00
bors
5bd9b60333 Auto merge of #79553 - sexxi-goose:mir_min_cap_writeback, r=nikomatsakis
Capture precise paths in THIR and MIR

This PR allows THIR and MIR to use the result of the new capture analysis to actually capture precise paths

To achieve we:
- Writeback min capture results to TypeckResults
- Move handling upvars to PlaceBuilder in mir_build
- Lower precise paths in THIR build by reading min_captures
- Search for ancestors in min_capture when trying to build a MIR place which starts off of an upvar

Closes: https://github.com/rust-lang/project-rfc-2229/issues/10

Partly implements: rust-lang/project-rfc-2229#18

Work that remains (not in this PR):
- [ ] [Known bugs when feature gate is enabled](https://github.com/rust-lang/project-rfc-2229/projects/1?card_filter_query=label%3Abug)
- [ ] Use min_capure_map for
  - [ ] Liveness analysis
  - [ ] rustc_mir/interpret/validity.rs
  - [ ] regionck
- [ ] rust-lang/project-rfc-2229#8
- [ ] remove closure_captures and upvar_capture_map

r? `@ghost`
2020-12-12 00:23:29 +00:00
Matthias Krüger
b7795e135a fix clippy::{needless_bool, manual_unwrap_or} 2020-12-11 23:02:19 +01:00
Matthias Krüger
db6c50998c don't clone types that are copy (clippy::clone_on_copy) 2020-12-11 23:02:19 +01:00
Jack Huey
ed80815bf2 Move binder for dyn to each list item 2020-12-11 15:02:46 -05:00
Tyler Mandry
f3a3fc900c
Rollup merge of #79639 - sasurau4:feature/add-long-explanation-E0212, r=GuillaumeGomez
Add long explanation for E0212

Helps with #61137
2020-12-10 21:33:04 -08:00
Tyler Mandry
666d1a8951
Rollup merge of #79824 - LingMan:no_replace, r=lcnr
Strip prefix instead of replacing it with empty string

r? `@lcnr,` since you reviewed my other PR in the area.
`@rustbot` modify labels +C-cleanup +T-compiler
2020-12-09 13:38:31 -08:00
bors
5e6e1e33a1 Auto merge of #79817 - LingMan:if_map, r=lcnr
Replace simple `if let` constructs with Option::map

Replaces a few constructs of the form

```
if let Some(x) = var {
    Some(...)
} else {
    None
}
```

with calls to `Option::map`.

`@rustbot` modify labels +C-cleanup +T-compiler
2020-12-08 13:58:15 +00:00
LingMan
06aa7a7601 Strip prefix instead of replacing it with empty string 2020-12-08 14:46:19 +01:00
bors
4fd4a98d47 Auto merge of #79806 - LeSeulArtichaut:fixup-filter-is-none, r=jyn514
Fixup: `filter().is_none()` -> `!any()`
2020-12-08 08:51:51 +00:00
LingMan
af9402af0f Replace simple if let constructs with Option::map
Replaces a few constructs of the form

if let Some(x) = var {
    Some(...)
} else {
    None
}

with calls to Option::map.
2020-12-08 02:40:14 +01:00
bors
bda05cc471 Auto merge of #79653 - tmiasko:naked-functions, r=Amanieu
Validate naked functions definitions

Validate that naked functions are defined in terms of a single inline assembly
block that uses only `const` and `sym` operands and has `noreturn` option.

Implemented as future incompatibility lint with intention to migrate it into
hard error. When it becomes a hard error it will ensure that naked functions are
either unsafe or contain an unsafe block around the inline assembly. It will
guarantee that naked functions do not reference functions parameters (obsoleting
part of existing checks from #79411). It will limit the definitions of naked
functions to what can be reliably supported. It will also reject naked functions
implemented using legacy LLVM style assembly since it cannot satisfy those
conditions.

https://github.com/rust-lang/rfcs/pull/2774
https://github.com/rust-lang/rfcs/pull/2972
2020-12-07 22:47:20 +00:00
LeSeulArtichaut
9cc563b70b Fixup: filter().is_none() -> !any() 2020-12-07 21:40:20 +01:00
bors
b5ff9c3d05 Auto merge of #79773 - lcnr:type-visitor, r=oli-obk
small `TypeVisitor` refactor

cc `@LeSeulArtichaut` `@scottmcm`

adds `ControlFlow::map_break`
2020-12-07 15:07:09 +00:00
Bastian Kauschke
e3e4870bce small TypeVisitor refactor 2020-12-07 15:52:59 +01:00
bors
2b76c48328 Auto merge of #79772 - ethanboxx:79760-wrongly-speaks-of-methods, r=oli-obk
smarter E0390

Should fix #79760

I am fairly new to the compiler so am hoping I did things correctly :).
2020-12-07 09:28:25 +00:00