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

Use more slice patterns inside the compiler #128762

Merged
merged 1 commit into from
Aug 11, 2024

Conversation

fmease
Copy link
Member

@fmease fmease commented Aug 7, 2024

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 added the C-cleanup Category: PRs that clean code up or issues documenting cleanup. label Aug 7, 2024
@rustbot rustbot 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 fmease marked this pull request as ready for review August 7, 2024 01:49
@rustbot
Copy link
Collaborator

rustbot commented Aug 7, 2024

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
Member Author

fmease commented Aug 7, 2024

r? compiler

@rust-log-analyzer

This comment has been minimized.

@@ -585,7 +585,7 @@ impl Pat {
}
// 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
Member

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.

@@ -292,9 +292,9 @@ impl<'tcx> Cx<'tcx> {
} else {
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
Member

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.

@@ -898,8 +898,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
self.param_env,
adt_def.non_enum_variant().fields[field].ty(self.tcx, args),
);
if fields.len() == 1 {
let src_ty = fields.raw[0].ty(self.body, self.tcx);
if let [field] = &*fields.raw {
Copy link
Member

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 fmease marked this pull request as draft August 7, 2024 10:31
@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
@fmease fmease added the rla-silenced Silences rust-log-analyzer postings to the PR it's added on. label Aug 7, 2024
@fmease fmease 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
Member

@bors r+

@bors
Copy link
Contributor

bors commented Aug 10, 2024

📌 Commit c4c518d has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors 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
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:master Aug 11, 2024
6 checks passed
@rustbot rustbot 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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) C-cleanup Category: PRs that clean code up or issues documenting cleanup. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant 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