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

Add basic support for array lengths in types #8799

Merged
merged 2 commits into from
May 12, 2021
Merged

Conversation

lf-
Copy link
Contributor

@lf- lf- commented May 11, 2021

This recognizes let a = [1u8, 2, 3] as having type [u8; 3] instead
of the previous [u8; _]. Byte strings and [0u8; 2] kinds of range
array declarations are unsupported as before.

I don't know why a bunch of our rustc tests had single quotes inside
strings un-escaped by UPDATE_EXPECT=1 cargo t, but I don't think it's
bad? Maybe something in a nightly?

@lf-
Copy link
Contributor Author

lf- commented May 11, 2021

the ci comes back i see, those were indeed not to be updated, yet. well, fyi, some nightly will break these tests when you update ;-)

Should I revert my updates to those? going to do it, since it will probably be a while until this change gets to stable

This recognizes `let a = [1u8, 2, 3]` as having type `[u8; 3]` instead
of the previous `[u8; _]`. Byte strings and `[0u8; 2]` kinds of range
array declarations are unsupported as before.

I don't know why a bunch of our rustc tests had single quotes inside
strings un-escaped by `UPDATE_EXPECT=1 cargo t`, but I don't think it's
bad? Maybe something in a nightly?
@flodiebold
Copy link
Member

Good idea to start with array expressions, that sidesteps the whole const expression body lowering topic. I'll probably review this this evening.

@lf-
Copy link
Contributor Author

lf- commented May 11, 2021

I don't know why a bunch of our rustc tests had single quotes inside
strings un-escaped by UPDATE_EXPECT=1 cargo t

I think that means you're on an older Rust version

Quite the opposite, in fact :) Today's nightly. But I'm reverting it because it's not right for stable, which is nominally the target for the tests.

crates/hir_ty/src/consts.rs Outdated Show resolved Hide resolved
Comment on lines +254 to +256
// we were previously assuming this to be true, I'm not whether true or false on
// unknown values is safer.
(_, _) => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this is where you want to "defer" an obligation while returning true so that it's "conditionally true" (and if the condition is the same equality check then it's basically "unknowable", instead of "known true/false"), but @nikomatsakis or someone else familiar with Chalk would have to explain how that integrates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under which conditions does this happen in rustc?

@flodiebold
Copy link
Member

LGTM. I'd be interested in what @nikomatsakis or @rust-lang/wg-traits think about handling unknown/unevaluatable consts though.

Also, if you haven't yet, would you mind running rust-analyzer analysis-stats . at least on the rust-analyzer repo itself to make sure it doesn't crash, and ideally compare the output before/after?

@lf-
Copy link
Contributor Author

lf- commented May 11, 2021

@lf-
Copy link
Contributor Author

lf- commented May 11, 2021

LGTM. I'd be interested in what @nikomatsakis or @rust-lang/wg-traits think about handling unknown/unevaluatable consts though.

Also, if you haven't yet, would you mind running rust-analyzer analysis-stats . at least on the rust-analyzer repo itself to make sure it doesn't crash, and ideally compare the output before/after?

new:

Database loaded:     470.55ms, 256minstr
  crates: 34, mods: 661, decls: 12808, fns: 10109
Item Collection:     7.79s, 67ginstr
  exprs: 279785, ??ty: 2522 (0%), ?ty: 1823 (0%), !ty: 1082
Inference:           17.38s, 103ginstr
Total:               25.17s, 170ginstr

old:

Database loaded:     621.49ms, 258minstr
  crates: 34, mods: 660, decls: 12804, fns: 10107
Item Collection:     7.84s, 67ginstr
  exprs: 279717, ??ty: 2519 (0%), ?ty: 1814 (0%), !ty: 1081
Inference:           17.85s, 103ginstr
Total:               25.68s, 170ginstr

I'm going to consider that likely an insignificant difference, which does make a fair bit of sense given we are not doing much work to achieve this.

@lf-
Copy link
Contributor Author

lf- commented May 12, 2021

I've addressed the actionable review feedback.

Currently I'm about halfway through doing #2834 (comment), #540, etc, to be able to parse literals in RA, which we can use to add lengths to byte strings and to cheat on putting lengths on arrays with literal lengths. This work will be in a future PR based on this one for ease of review and I'll file that when I get it done.

@flodiebold
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented May 12, 2021

@bors bors bot merged commit 312f1fe into rust-lang:master May 12, 2021
bors bot added a commit that referenced this pull request May 16, 2021
8813: Get some more array lengths! r=lf- a=lf-

This is built on #8799 and thus contains its changes. I'll rebase it onto master when that one gets merged. It adds support for r-a understanding the length of:

* `let a: [u8; 2] = ...`
* `let a = b"aaa"`
* `let a = [0u8; 4]`

I have added support for getting the values of byte strings, which was not previously there. I am least confident in the correctness of this part and it probably needs some more tests, as we currently have only one test that exercised that part (!).

Fixes #2922.

Co-authored-by: Jade <software@lfcode.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants