Skip to content

[Bug]: assign after await tersify with a behavioural logic change #9959

Description

@paoloricciuti

Reproduction link or steps

We got this bug report in svelte sveltejs/svelte#18454 but after investigation the issue is actually from rolldown.

Having code like this

let waitWithInitPromise = waitWithInit();

let var1 = 0;

// Simulate a network request
async function wait(): Promise<number> {
    await new Promise((resolve) => setTimeout(resolve, 3000));
    return 1;
}

async function waitWithInit(): Promise<void> {
    const num = await wait();

    var1 = num;
}

console.log(waitWithInitPromise, var1)

As you can see in this REPL rollodown compress

async function waitWithInit(): Promise<void> {
    const num = await wait();

    var1 = num;
}

to

async function waitWithInit() {
	var1 = await wait();
}

but that's a behavioural change because before you access var1 for the first time after the await and after you access it before.

What is expected?

async function waitWithInit(): Promise<void> {
    const num = await wait();

    var1 = num;
}

after tersing should not change the code so that var1 is accessed before the await

What is actually happening?

async function waitWithInit(): Promise<void> {
    const num = await wait();

    var1 = num;
}

is compiled to

async function waitWithInit() {
	var1 = await wait();
}

which make so that var1 is accessed before the await.

System Info

repl

Any additional comments?

No response

Metadata

Metadata

Assignees

Type

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions