Skip to content

Make [$expr; 0] equivalent to { drop($expr); [] } - #165986

Open
Jules-Bertholet wants to merge 2 commits into
rust-lang:mainfrom
Jules-Bertholet:array-repeat-experiment
Open

Jules-Bertholet wants to merge 2 commits into
rust-lang:mainfrom
Jules-Bertholet:array-repeat-experiment

Conversation

@Jules-Bertholet

@Jules-Bertholet Jules-Bertholet commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #165323.

Potentially changes runtime behavior of existing code!!! Needs a crater test run.

@rustbot label A-array T-lang needs-fcp

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-array Area: `[T; N]` needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. T-lang Relevant to the language team labels Nov 15, 2026
@Jules-Bertholet
Jules-Bertholet marked this pull request as ready for review November 15, 2026 06:30
@rustbot

rustbot commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @vakaras

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 15, 2026
@rustbot

rustbot commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

@theemathas

Copy link
Copy Markdown
Contributor

Does this PR change temporary lifetime extension behavior?

@rust-log-analyzer

This comment has been minimized.

@bjorn3

bjorn3 commented Nov 15, 2026

Copy link
Copy Markdown
Member

The mental model here is that [$expr; N] evaluates $expr to a place exactly once, and then moves/copies out of that place N times.

Are you following the same mental model for const blocks? If so why would [const { Vec::<u8>::new() }; 3] work?

@RalfJung

Copy link
Copy Markdown
Member

The mental model here is that [$expr; N] evaluates $expr to a place exactly once, and then moves/copies out of that place N times.

I'm not a fan. This is a value expression context, not a place expression context, so we should not apply place expression rules.

@theemathas

Copy link
Copy Markdown
Contributor

This PR introduces a discrepancy between arrays with a generic N length where N = 0, and arrays which have a literal 0length. For example:

struct LoudDrop;
impl Drop for LoudDrop {
    fn drop(&mut self) {
        println!("dropped");
    }
}

const C: LoudDrop = LoudDrop;

fn foo<const N: usize>() {
    let _ = [C; N];
}

fn bar() {
    let _ = [C; 0];
}

fn main() {
    println!("foo");
    foo::<0>();
    println!("bar");
    bar();
}

Output on nightly:

foo
bar

Output with this PR:

foo
bar
dropped

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

@theemathas whoops… I guess this is harder to implement than I thought. Marking as draft for now

@Jules-Bertholet
Jules-Bertholet marked this pull request as draft November 15, 2026 19:08
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 15, 2026
@Jules-Bertholet
Jules-Bertholet marked this pull request as ready for review November 15, 2026 20:50
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 15, 2026
@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

@theemathas Your example works properly now. We just generate an if N == 0 { drop(C) } for const operands; that seems to be the easiest way.

Are you following the same mental model for const blocks? If so why would [const { Vec::<u8>::new() }; 3] work?

Because we special-case it to work. Copying non-Copy values an arbitrary number of times is usually not allowed, but if the value is a const we know it's OK. (I don't think there is any possible model that doesn't special-case consts at least a little bit.)

This is a value expression context, not a place expression context, so we should not apply place expression rules.

We can say that it is a place expression context after all. We get to decide that.

@RalfJung

RalfJung commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

We can say that it is a place expression context after all. We get to decide that.

There's no rhyme or reason to this though, as far as I can see. Place expression contexts are those that actually need a place. This here only needs a value.

What's the motivation for making it a place expression, other than "it's a hack that makes the delta to the previous behavior smaller"? We haven't yet seen evidence that going all the way to consistent value-based behavior would be problematic, so I don't consider that argument convincing.

I think this PR is the wrong fix for the problem; I described my preferred fix here.

@Jules-Bertholet

Jules-Bertholet commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

makes the delta to the previous behavior smaller

It doesn't, actually, it makes the delta bigger. I think that is worth it for a more consistent mental model (conditional on Crater, of course).

This here only needs a value

… sometimes. Sometimes (when the length is 0) we don't need the value at all. IMO, it's natural for the semantics to reflect that.

(In your comment, you seem to imply that needing "only" a value is a weaker requirement than needing a place. IMO it's the other way around. A value can always be turned into a place (make a temporary), but making a value from a place is not always possible.)

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Does this PR change temporary lifetime extension behavior?

It shouldn't—added fixes and tests for this.

@jieyouxu

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned mejrs and unassigned jieyouxu Nov 16, 2026
@RalfJung

RalfJung commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

In my eyes this PR makes the mental model strictly less consistent than my suggestion.

Sometimes (when the length is 0) we don't need the value at all. IMO, it's natural for the semantics to reflect that.

It is up to us to define what we need and don't need.

If we are talking about what the semantics strictly need, we need a place if we want to create a pointer or mutate. Neither happens here, so creating a place makes no sense whatsoever. Values are the more natural choice for an expression to evaluate to, so IMO picking a place instead always needs justification. So far the Rust semantics follows the principle that we only evaluate to a place if we actually need a place. I am opposed to this PR because it gives up on that principle for no good reason.

In match place { _ => ... }, we always evaluate the argument as a place, so it is perfectly natural to also do so when the pattern is _. But having ad-hoc rules where the value of the array length determines whether we compute a place or a value? I cannot see any justification for that.

@RalfJung

RalfJung commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

(In your comment, you see to imply that needing "only" a value is a weaker requirement than needing a place.

I am not implying anything of the sort. If anything I am implying the opposite, I guessed that you made this PR because you consider "only" evaluating a place to be a worthy hack to make the semantics change less invasive. It is the only explanation I can imagine for why one would want to add a new place context here. If that's not the explanation then I am even more strongly opposed to this PR, it feels just fully arbitrary.^^

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

With value semantics, dropping the value on N = 0 is an awkward special case. With place semantics, it's a natural consequence of making a temporary and then not moving out of it. That's my intuition for why using a place feels cleaner to me.

@Jules-Bertholet Jules-Bertholet changed the title Make [$expr; 0] equivalent to { let _ = $expr; [] } Make [$expr; 0] equivalent to { drop($expr); [] } Nov 16, 2026
@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

@RalfJung I've come around to your position. What changed my mind is code like this:

let mut x = [42i32; N];
x = [*&x[0]; N];

This needs to go through an intermediate temporary somewhere. Always making that temporary is more consistent.

@RalfJung

Copy link
Copy Markdown
Member

I don't think I quite understand the significance of the example... but I am happy if we can agree on the semantics. :)

With value semantics, dropping the value on N = 0 is an awkward special case. With place semantics, it's a natural consequence of making a temporary and then not moving out of it. That's my intuition for why using a place feels cleaner to me.

It's kind of the same special case as

{ 
  let x = <expr>;
  if N > 0 { /* move out of x */ }
  // drop x if N = 0
}

so I don't think it is comparable in specialness to using place evaluation here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-array Area: `[T; N]` needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. 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. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A !-typed constant in a zero-length array [C; 0] is never evaluated, but type checking treats it as diverging, so safe code executes unreachable

8 participants