Skip to content

Use more slice patterns inside the compiler - #128762

Merged
bors merged 1 commit into
rust-lang:masterfrom
fmease:use-more-slice-pats
Aug 11, 2024
Merged

Use more slice patterns inside the compiler#128762
bors merged 1 commit into
rust-lang:masterfrom
fmease:use-more-slice-pats

Conversation

@fmease

Copy link
Copy Markdown
Member

Nothing super noteworthy. Just replacing the common 'fragile' pattern of "length check followed by indexing or unwrap" with slice patterns for legibility and 'robustness'.

r? ghost

@fmeasefmease added the C-cleanup Category: PRs that clean code up or issues documenting cleanup. label Aug 7, 2024
@rustbotrustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 7, 2024
@rust-log-analyzer

This comment has been minimized.

@fmease
fmeaseforce-pushed the use-more-slice-pats branch from 0257ce3 to 8868f91CompareAugust 7, 2024 01:47
@fmease
fmease marked this pull request as ready for review August 7, 2024 01:49
@rustbot

Copy link
Copy Markdown
Collaborator

Some changes occurred in match checking

cc @Nadrieril

Some changes occurred in coverage instrumentation.

cc @Zalathar

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

These commits modify compiler targets.
(See the Target Tier Policy.)

@fmease

Copy link
Copy Markdown
MemberAuthor

r? compiler

@rust-log-analyzer

This comment has been minimized.

Comment threadcompiler/rustc_ast/src/ast.rs Outdated
// A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
// when `P` can be reparsed as a type `T`.
PatKind::Slice(pats) if pats.len() == 1 => pats[0].to_ty().map(TyKind::Slice)?,
PatKind::Slice(pats) if let [pat] = &**pats => pat.to_ty().map(TyKind::Slice)?,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would kinda prefer we spell this as pats.as_slice() or &pats[..].

&** is kinda an eyesore, and the implicit autoderef of either strategies makes it less annoying if we change around the underlying type reprs.

let attrs = tcx.hir().attrs(expr.hir_id);
if attrs.iter().any(|a| a.name_or_empty() == sym::rustc_box) {
if attrs.len() != 1 {
if let [attr] = attrs {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You need to invert this check.

);
if fields.len() == 1 {
let src_ty = fields.raw[0].ty(self.body, self.tcx);
if let [field] = &*fields.raw {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:( this is kinda annoying to have to do, but I guess at least equally annoying as fields.raw[0].

@fmease
fmeaseforce-pushed the use-more-slice-pats branch from 8868f91 to b6070a0CompareAugust 7, 2024 10:29
@fmease
fmease marked this pull request as draft August 7, 2024 10:31
@fmease
fmeaseforce-pushed the use-more-slice-pats branch from b6070a0 to a3e3ecaCompareAugust 7, 2024 10:54
@fmease
fmease marked this pull request as ready for review August 7, 2024 11:00
@rust-log-analyzer

This comment has been minimized.

@fmease
fmease marked this pull request as draft August 7, 2024 11:31
@fmeasefmease added the rla-silenced Silences rust-log-analyzer postings to the PR it's added on. label Aug 7, 2024
@fmease
fmeaseforce-pushed the use-more-slice-pats branch from a3e3eca to c4c518dCompareAugust 7, 2024 11:38
@fmeasefmease removed the rla-silenced Silences rust-log-analyzer postings to the PR it's added on. label Aug 7, 2024
@fmease
fmease marked this pull request as ready for review August 7, 2024 12:47
@compiler-errors

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Aug 10, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit c4c518d has been approved by compiler-errors

It is now in the queue for this repository.

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 10, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 11, 2024
…iler-errors
Use more slice patterns inside the compiler
Nothing super noteworthy. Just replacing the common 'fragile' pattern of "length check followed by indexing or unwrap" with slice patterns for legibility and 'robustness'.
r? ghost
This was referenced Aug 11, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 11, 2024
…iaskrgr
Rollup of 6 pull requests
Successful merges:
- rust-lang#120314 (core: optimise Debug impl for ascii::Char)
- rust-lang#128536 (Preliminary cleanup of `WitnessPat` hoisting/printing)
- rust-lang#128592 (Promote aarch64-apple-darwin to Tier 1)
- rust-lang#128762 (Use more slice patterns inside the compiler)
- rust-lang#128875 (rm `import.used`)
- rust-lang#128882 (make LocalWaker::will_wake consistent with Waker::will_wake)
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 32e0fe1 into rust-lang:masterAug 11, 2024
@rustbotrustbot added this to the 1.82.0 milestone Aug 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 11, 2024
Rollup merge of rust-lang#128762 - fmease:use-more-slice-pats, r=compiler-errors
Use more slice patterns inside the compiler
Nothing super noteworthy. Just replacing the common 'fragile' pattern of "length check followed by indexing or unwrap" with slice patterns for legibility and 'robustness'.
r? ghost
@fmease
fmease deleted the use-more-slice-pats branch August 11, 2024 12:08
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-query-systemArea: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html)C-cleanupCategory: PRs that clean code up or issues documenting cleanup.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@fmease@rust-log-analyzer@rustbot@compiler-errors@bors@davidtwco