Backtestsvuerun-20261003-231210#15048
Scoped slot transforms array argument if rendered conditionally
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The bug is that `<slot v-if v-bind="items">` compiles to `mergeProps({key:0}, items)`, which turns an array into an object. Both patches fix this by passing the branch key to `renderSlot` as a separate 6th argument, and both keep user-provided `props.key` higher in priority at runtime.
**Patch B is narrower and therefore lower risk.**
- It only reroutes the key in the one case that causes the bug: a slot outlet whose props are a single bare `v-bind` expression (the final `else` branch of `injectProp`).
- Every other path keeps today's codegen. A plain `<slot v-if>` still compiles to `{ key: 0 }`, so no existing snapshots change.
- It also stops the `ref_for` marker from being merged into slot-outlet props. That marker causes the same array-to-object conversion for `<slot v-for v-bind="item">`. This closely related fix carries little risk, because slot outlets do not use refs.
**Patch B's tests are stronger.**
- Its compiler tests cover `v-if`/`v-else`, the fallback-plus-`scopeId` argument padding, and `template v-for` with `:key`.
- Its runtime tests cover props identity and key priority.
- Crucially, it adds end-to-end tests in the `vue` package. These render the issue's exact scenario and check that `Array.isArray(props)` is true after toggling the branch. These tests would fail before the fix.
**Patch A changes much more than the bug requires.**
- It reroutes every key injection on slot outlets, including slots with no props.
- As a result it rewrites existing snapshots and test expectations (`{key:0}` becomes `{}, undefined, undefined, 0`).
- Its snapshot diff for `transformElement.spec.ts.snap` looks wrong: it adds a v-for test snapshot to the transformElement snapshot file, with the same index hashes as the vFor snap file. This looks like stray churn.
- Its AST type changes are broader than needed.
- It does not address the `ref_for` merge path.
Both patches are correct for the reported case, but B is more targeted, better tested end to end, and avoids A's churn.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
12m 40s
Wall-clock time in the container
Full test suite
No new failures
3,630 passed · 6 skipped · 3,636 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
250vs 266 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.0
Link to minimal reproduction
Steps to reproduce
- Create a component with a scoped slot that is rendered conditionally
- Pass an array as the sole argument to the slot
What is expected?
Array is rendered normally.
If you render items of that array in v-for loop, you only see items inside that array
What is actually happening?
Array is transformed into object. Items and their indexes are staying the same, key field is added
<img width="383" height="111" alt="Image" src="https://github.com/user-attachments/assets/338bc873-5c20-4fb3-aed9-88f9f8e8cd98" />
System Info
Any additional comments?
key attribute is generated by the compiler for the slot rendered with v-if.
<img width="781" height="243" alt="Image" src="https://github.com/user-attachments/assets/99704cf9-71cb-435d-ba6f-92578b65ff5d" />
While this is not explicitly mentioned in documentation, one can assume that the expectation is that slotProps is always an object, as this is what will be passed to the slot if you use multiple v-bind directives on the slot.
This issue is rather pointing out the difference in behavior of the slot when v-if is present or not present.
The two patches
packages/compiler-core/__tests__/transforms/vFor.spec.tsindex 34991445c..b351ecb54 100644@@ -918,6 +918,55 @@ describe('compiler: v-for', () => { expect(generate(root).code).toMatchSnapshot() }) + test('template v-for key injection w/ <slot v-bind/>', () => {+ const {+ root,+ node: { codegenNode },+ } = parseWithForTransform(+ '<template v-for="item in list" :key="item.id"><slot v-bind="item.data"/></template>',+ )+ // the key must not be merged into the slot props, otherwise+ // non-object values (e.g. arrays) would be converted into objects+ expect(+ assertSharedCodegen(codegenNode, true, true /* custom return */),+ ).toMatchObject({+ source: { content: `list` },+ params: [{ content: `item` }],+ returns: {+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: RENDER_SLOT,+ arguments: [+ '$slots',+ '"default"',+ {+ type: NodeTypes.JS_CALL_EXPRESSION,+ arguments: [+ {+ type: NodeTypes.JS_CALL_EXPRESSION,+ arguments: [{ content: `item.data` }],+ },+ ],+ },+ 'undefined',+ 'undefined',+ { content: `item.id` },+ ],+ },+ })+ expect(generate(root).code).toMatch(+ `_renderSlot($slots, "default", _normalizeProps(_guardReactiveProps(item.data)), undefined, undefined, item.id)`,+ )+ })++ test('v-for on <slot v-bind/> should not merge ref_for marker into props', () => {+ const { root } = parseWithForTransform(+ '<slot v-for="item in list" v-bind="item.data"/>',+ )+ expect(generate(root).code).toMatch(+ `_renderSlot($slots, "default", _normalizeProps(_guardReactiveProps(item.data)))`,+ )+ })+ // #1907 test('template v-for key injection with single child', () => { const {packages/compiler-core/__tests__/transforms/vIf.spec.tsindex fe3aef714..2f3cceb57 100644@@ -26,6 +26,7 @@ import { import { CREATE_COMMENT, FRAGMENT,+ GUARD_REACTIVE_PROPS, MERGE_PROPS, NORMALIZE_PROPS, RENDER_SLOT,@@ -675,6 +676,76 @@ describe('compiler: v-if', () => { }) }) + test('key injection on <slot/> (only v-bind)', () => {+ const {+ root,+ node: { codegenNode },+ } = parseWithIfTransform(+ `<slot v-if="ok" v-bind="items"/><slot v-else v-bind="items"/>`,+ )+ // the key must not be merged into the slot props, otherwise+ // non-object values (e.g. arrays) would be converted into objects+ const slotProps = {+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: NORMALIZE_PROPS,+ arguments: [+ {+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: GUARD_REACTIVE_PROPS,+ arguments: [{ content: `items` }],+ },+ ],+ }+ expect(codegenNode.consequent).toMatchObject({+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: RENDER_SLOT,+ arguments: [+ '$slots',+ '"default"',+ slotProps,+ 'undefined',+ 'undefined',+ { content: `0` },+ ],+ })+ expect(codegenNode.alternate).toMatchObject({+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: RENDER_SLOT,+ arguments: [+ '$slots',+ '"default"',+ slotProps,+ 'undefined',+ 'undefined',+ { content: `1` },+ ],+ })+ expect(generate(root).code).toMatch(+ `_renderSlot($slots, "default", _normalizeProps(_guardReactiveProps(items)), undefined, undefined, 0)`,+ )+ })++ test('key injection on <slot/> (only v-bind, w/ fallback & scopeId)', () => {+ const {+ node: { codegenNode },+ } = parseWithIfTransform(+ `<slot v-if="ok" v-bind="items">fallback</slot>`,+ { scopeId: 'data-v-xxx', slotted: false },+ )+ expect(codegenNode.consequent).toMatchObject({+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: RENDER_SLOT,+ arguments: [+ '$slots',+ '"default"',+ { callee: NORMALIZE_PROPS },+ { type: NodeTypes.JS_FUNCTION_EXPRESSION },+ 'true',+ { content: `0` },+ ],+ })+ })+ test('key injection (before v-bind)', () => { const { node: { codegenNode },packages/compiler-core/src/ast.tsindex e3cc5304d..03b374145 100644@@ -488,7 +488,7 @@ export interface DirectiveArgumentNode extends ArrayExpression { export interface RenderSlotCall extends CallExpression { callee: typeof RENDER_SLOT arguments:- // $slots, name, props, fallback+ // $slots, name, props, fallback, noSlotted, key | [string, string | ExpressionNode] | [string, string | ExpressionNode, PropsExpression] | [@@ -497,6 +497,14 @@ export interface RenderSlotCall extends CallExpression { PropsExpression | '{}', TemplateChildNode[], ]+ | [+ string,+ string | ExpressionNode,+ PropsExpression | '{}',+ FunctionExpression | 'undefined',+ 'true' | 'undefined',+ JSChildNode,+ ] } export type SlotsExpression = SlotsObjectExpression | DynamicSlotsExpressionpackages/compiler-core/src/transforms/transformElement.tsindex 40f08d45b..d7fa2d459 100644@@ -638,7 +638,12 @@ export function buildProps( } // #10696 in case a v-bind object contains ref- pushRefVForMarker()+ // skipped for slot outlets: their props are passed to the slot+ // function as-is, and merging the marker in would convert+ // non-object values (e.g. arrays) into objects+ if (node.tagType !== ElementTypes.SLOT) {+ pushRefVForMarker()+ } pushMergeArg() mergeArgs.push(exp) } else {packages/compiler-core/src/utils.tsindex aa426bbab..f04c1d069 100644@@ -445,6 +445,15 @@ export function injectProp( props.properties.unshift(prop) } propsWithInjection = props+ } else if (node.type === NodeTypes.JS_CALL_EXPRESSION) {+ // single v-bind with expression on a slot outlet, e.g.+ // `<slot v-if="ok" v-bind="items" />`. Merging the key into the props+ // would convert non-plain-object values (e.g. arrays) into objects, so the+ // key is passed to `renderSlot` as a separate argument instead.+ const args = node.arguments as CallExpression['arguments']+ while (args.length < 5) args.push('undefined')+ args[5] = prop.value+ return } else { // single v-bind with expression, return a merged replacement propsWithInjection = createCallExpression(context.helper(MERGE_PROPS), [packages/runtime-core/__tests__/helpers/renderSlot.spec.tsindex bc0678b51..a9502232b 100644@@ -38,6 +38,38 @@ describe('renderSlot', () => { expect(vnode.key).toBe('_default') }) + it('should use separately passed key without merging it into props', () => {+ const items = ['a', 'b']+ let received: any+ const vnode = renderSlot(+ {+ default: (props: any) => {+ received = props+ return [h('div')]+ },+ },+ 'default',+ items as any,+ undefined,+ undefined,+ 1,+ )+ expect(received).toBe(items)+ expect(vnode.key).toBe('1')+ })++ it('should prefer key in slot props over separately passed key', () => {+ const vnode = renderSlot(+ { default: () => [h('div')] },+ 'default',+ { key: 'foo' },+ undefined,+ undefined,+ 0,+ )+ expect(vnode.key).toBe('foo')+ })+ it('should render slot fallback', () => { const vnode = renderSlot({}, 'default', { key: 'foo' }, () => ['fallback']) expect(vnode.children).toEqual(['fallback'])packages/runtime-core/src/helpers/renderSlot.tsindex 8f65f8af4..f9f75d06c 100644@@ -16,7 +16,7 @@ import { isVNode, openBlock, } from '../vnode'-import { PatchFlags, SlotFlags, isSymbol } from '@vue/shared'+import { PatchFlags, SlotFlags, extend, isSymbol } from '@vue/shared' import { warn } from '../warning' import { isAsyncWrapper } from '../apiAsyncComponent' @@ -32,6 +32,10 @@ export function renderSlot( // the compiler and guaranteed to be a function returning an array fallback?: () => VNodeArrayChildren, noSlotted?: boolean,+ // branch key injected by the compiler for `v-if` / keyed `v-for` slot+ // outlets whose props are a single `v-bind` expression, passed separately+ // so that non-object props (e.g. arrays) are not merged into an object+ key?: PropertyKey, ): VNode { if ( currentRenderingInstance!.ce ||@@ -39,6 +43,8 @@ export function renderSlot( isAsyncWrapper(currentRenderingInstance!.parent) && currentRenderingInstance!.parent.ce) ) {+ // a compiler-injected branch key becomes a prop of the slot outlet+ if (key != null) props = extend({ key }, props) const hasProps = Object.keys(props).length > 0 // in custom element mode, render <slot/> as actual slot outlets // wrap it with a fragment because in shadowRoot: false mode the slot@@ -79,7 +85,8 @@ export function renderSlot( try { const validSlotContent = slot && ensureValidVNode(slot(props)) const slotKey =- props.key ||+ (props && props.key) ||+ key || // slot content array of a dynamic conditional slot may have a branch // key attached in the `createSlots` helper, respect that (validSlotContent && (validSlotContent as any).key)packages/vue/__tests__/index.spec.tsindex 6eeff41dd..07600023a 100644@@ -293,6 +293,67 @@ describe('compiler + runtime integration', () => { expect(container.innerHTML).toBe(`<div>2<div>1</div></div>`) }) + test('array slot props with v-if on <slot>', async () => {+ const ok = ref(true)+ const Comp = {+ setup() {+ return { items: ['a', 'b', 'c'], ok }+ },+ template:+ `<div><slot v-bind="items"/></div>` ++ `<div><slot v-if="ok" v-bind="items"/><slot v-else v-bind="items"/></div>`,+ }+ const App = {+ components: { Comp },+ template: `<Comp v-slot="props">{{ Array.isArray(props) }}-{{ JSON.stringify(props) }}</Comp>`,+ }+ const root = document.createElement('div')+ createApp(App).mount(root)+ const expected = `<div>true-["a","b","c"]</div>`.repeat(2)+ expect(root.innerHTML).toBe(expected)++ ok.value = false+ await nextTick()+ expect(root.innerHTML).toBe(expected)+ })++ test('array slot props with keyed <template v-for> wrapping <slot>', () => {+ const Comp = {+ setup() {+ return {+ list: [+ { id: 'x', data: [1, 2] },+ { id: 'y', data: [3] },+ ],+ }+ },+ template: `<div><template v-for="item in list" :key="item.id"><slot v-bind="item.data"/></template></div>`,+ }+ const App = {+ components: { Comp },+ template: `<Comp v-slot="props">{{ Array.isArray(props) }}-{{ JSON.stringify(props) }};</Comp>`,+ }+ const root = document.createElement('div')+ createApp(App).mount(root)+ expect(root.innerHTML).toBe(`<div>true-[1,2];true-[3];</div>`)+ })++ test('array slot props with v-for on <slot>', () => {+ const Comp = {+ setup() {+ return { list: [[1, 2], [3]] }+ },+ template: `<div><slot v-for="item in list" v-bind="item"/></div>`,+ }+ const App = {+ components: { Comp },+ template: `<Comp v-slot="props">{{ Array.isArray(props) }}-{{ JSON.stringify(props) }};</Comp>`,+ }+ const root = document.createElement('div')+ createApp(App).mount(root)+ expect(root.innerHTML).toBe(`<div>true-[1,2];true-[3];</div>`)+ })+ // #2413 it('EMPTY_ARR should not change', () => { const App = {
packages/compiler-core/__tests__/transforms/__snapshots__/transformElement.spec.ts.snapindex 3da778eb6..c07237de2 100644@@ -121,6 +121,20 @@ return function render(_ctx, _cache) { }" `; +exports[`compiler: v-for > codegen > template v-for key injection with single slot child 1`] = `+"const _Vue = Vue++return function render(_ctx, _cache) {+ with (_ctx) {+ const { renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createElementBlock: _createElementBlock, renderSlot: _renderSlot } = _Vue++ return (_openBlock(true), _createElementBlock(_Fragment, null, _renderList(items, (item) => {+ return _renderSlot($slots, "default", {}, undefined, undefined, item.id)+ }), 128 /* KEYED_FRAGMENT */))+ }+}"+`;+ exports[`compiler: v-for > codegen > template v-for w/ <slot/> 1`] = ` "const _Vue = Vue packages/compiler-core/__tests__/transforms/__snapshots__/vFor.spec.ts.snapindex 3da778eb6..c07237de2 100644@@ -121,6 +121,20 @@ return function render(_ctx, _cache) { }" `; +exports[`compiler: v-for > codegen > template v-for key injection with single slot child 1`] = `+"const _Vue = Vue++return function render(_ctx, _cache) {+ with (_ctx) {+ const { renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createElementBlock: _createElementBlock, renderSlot: _renderSlot } = _Vue++ return (_openBlock(true), _createElementBlock(_Fragment, null, _renderList(items, (item) => {+ return _renderSlot($slots, "default", {}, undefined, undefined, item.id)+ }), 128 /* KEYED_FRAGMENT */))+ }+}"+`;+ exports[`compiler: v-for > codegen > template v-for w/ <slot/> 1`] = ` "const _Vue = Vue packages/compiler-core/__tests__/transforms/__snapshots__/vIf.spec.ts.snapindex 65ee73c46..ed2c8e540 100644@@ -80,7 +80,7 @@ return function render(_ctx, _cache) { const { renderSlot: _renderSlot, createCommentVNode: _createCommentVNode } = _Vue return ok- ? _renderSlot($slots, "default", { key: 0 })+ ? _renderSlot($slots, "default", {}, undefined, undefined, 0) : _createCommentVNode("v-if", true) } }"@@ -140,7 +140,21 @@ return function render(_ctx, _cache) { const { renderSlot: _renderSlot, createCommentVNode: _createCommentVNode } = _Vue return ok- ? _renderSlot($slots, "default", { key: 0 })+ ? _renderSlot($slots, "default", {}, undefined, undefined, 0)+ : _createCommentVNode("v-if", true)+ }+}"+`;++exports[`compiler: v-if > codegen > v-if on <slot/> with v-bind 1`] = `+"const _Vue = Vue++return function render(_ctx, _cache) {+ with (_ctx) {+ const { normalizeProps: _normalizeProps, guardReactiveProps: _guardReactiveProps, renderSlot: _renderSlot, createCommentVNode: _createCommentVNode } = _Vue++ return ok+ ? _renderSlot($slots, "default", _normalizeProps(_guardReactiveProps(items)), undefined, undefined, 0) : _createCommentVNode("v-if", true) } }"packages/compiler-core/__tests__/transforms/vFor.spec.tsindex 34991445c..839d9d358 100644@@ -918,6 +918,34 @@ describe('compiler: v-for', () => { expect(generate(root).code).toMatchSnapshot() }) + test('template v-for key injection with single slot child', () => {+ const {+ root,+ node: { codegenNode },+ } = parseWithForTransform(+ '<template v-for="item in items" :key="item.id"><slot/></template>',+ )+ expect(+ assertSharedCodegen(codegenNode, true, true /* custom return */),+ ).toMatchObject({+ source: { content: `items` },+ params: [{ content: `item` }],+ returns: {+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: RENDER_SLOT,+ arguments: [+ '$slots',+ '"default"',+ '{}',+ 'undefined',+ 'undefined',+ { content: 'item.id' },+ ],+ },+ })+ expect(generate(root).code).toMatchSnapshot()+ })+ // #1907 test('template v-for key injection with single child', () => { const {packages/compiler-core/__tests__/transforms/vIf.spec.tsindex fe3aef714..677f6b55b 100644@@ -3,6 +3,7 @@ import { transform } from '../../src/transform' import { transformIf } from '../../src/transforms/vIf' import { transformElement } from '../../src/transforms/transformElement' import { transformSlotOutlet } from '../../src/transforms/transformSlotOutlet'+import { transformBind } from '../../src/transforms/vBind' import { type CommentNode, type ConditionalExpression,@@ -509,7 +510,14 @@ describe('compiler: v-if', () => { expect(codegenNode.consequent).toMatchObject({ type: NodeTypes.JS_CALL_EXPRESSION, callee: RENDER_SLOT,- arguments: ['$slots', '"default"', createObjectMatcher({ key: `[0]` })],+ arguments: [+ '$slots',+ '"default"',+ '{}',+ 'undefined',+ 'undefined',+ { content: '0' },+ ], }) expect(generate(root).code).toMatchSnapshot() })@@ -522,7 +530,41 @@ describe('compiler: v-if', () => { expect(codegenNode.consequent).toMatchObject({ type: NodeTypes.JS_CALL_EXPRESSION, callee: RENDER_SLOT,- arguments: ['$slots', '"default"', createObjectMatcher({ key: `[0]` })],+ arguments: [+ '$slots',+ '"default"',+ '{}',+ 'undefined',+ 'undefined',+ { content: '0' },+ ],+ })+ expect(generate(root).code).toMatchSnapshot()+ })++ test('v-if on <slot/> with v-bind', () => {+ const {+ root,+ node: { codegenNode },+ } = parseWithIfTransform(`<slot v-if="ok" v-bind="items"></slot>`, {+ directiveTransforms: {+ bind: transformBind,+ },+ })+ expect(codegenNode.consequent).toMatchObject({+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: RENDER_SLOT,+ arguments: [+ '$slots',+ '"default"',+ {+ type: NodeTypes.JS_CALL_EXPRESSION,+ callee: NORMALIZE_PROPS,+ },+ 'undefined',+ 'undefined',+ { content: '0' },+ ], }) expect(generate(root).code).toMatchSnapshot() })packages/compiler-core/src/ast.tsindex e3cc5304d..e6da01edb 100644@@ -488,14 +488,29 @@ export interface DirectiveArgumentNode extends ArrayExpression { export interface RenderSlotCall extends CallExpression { callee: typeof RENDER_SLOT arguments:- // $slots, name, props, fallback+ // $slots, name, props, fallback, noSlotted, branchKey | [string, string | ExpressionNode]- | [string, string | ExpressionNode, PropsExpression]+ | [string, string | ExpressionNode, PropsExpression | '{}'] | [ string, string | ExpressionNode, PropsExpression | '{}',- TemplateChildNode[],+ FunctionExpression | string,+ ]+ | [+ string,+ string | ExpressionNode,+ PropsExpression | '{}',+ FunctionExpression | string,+ string,+ ]+ | [+ string,+ string | ExpressionNode,+ PropsExpression | '{}',+ FunctionExpression | string,+ string,+ JSChildNode, ] } packages/compiler-core/src/utils.tsindex aa426bbab..025f6c32d 100644@@ -392,6 +392,10 @@ export function injectProp( prop: Property, context: TransformContext, ): void {+ if (node.type !== NodeTypes.VNODE_CALL && injectSlotKey(node, prop)) {+ return+ }+ let propsWithInjection: ObjectExpression | CallExpression | undefined /** * 1. mergeProps(...)@@ -473,6 +477,36 @@ export function injectProp( } } +function injectSlotKey(node: RenderSlotCall, prop: Property): boolean {+ if (+ prop.key.type !== NodeTypes.SIMPLE_EXPRESSION ||+ prop.key.content !== 'key'+ ) {+ return false+ }++ // If the slot already has a user-provided key, avoid injecting the branch+ // key so the user key keeps its existing priority.+ const props = node.arguments[2]+ if (props && !isString(props)) {+ const [unnormalizedProps] = getUnnormalizedProps(props as PropsExpression)+ if (+ unnormalizedProps &&+ !isString(unnormalizedProps) &&+ unnormalizedProps.type === NodeTypes.JS_OBJECT_EXPRESSION &&+ hasProp(prop, unnormalizedProps)+ ) {+ return true+ }+ }++ node.arguments[2] ||= '{}'+ node.arguments[3] ||= 'undefined'+ node.arguments[4] ||= 'undefined'+ node.arguments[5] = prop.value+ return true+}+ // check existing key to avoid overriding user provided keys function hasProp(prop: Property, props: ObjectExpression) { let result = falsepackages/runtime-core/__tests__/helpers/renderSlot.spec.tsindex bc0678b51..f359f431c 100644@@ -38,6 +38,83 @@ describe('renderSlot', () => { expect(vnode.key).toBe('_default') }) + it('should allow symbol values for compiler-injected slot keys', () => {+ const key = Symbol()+ const vnode = renderSlot(+ { default: () => [h('div')] },+ 'default',+ {},+ undefined,+ undefined,+ key,+ )+ expect(vnode.key).toBe('_default')+ })++ it('should not expose compiler-injected slot keys to slot props', () => {+ let receivedProps: any+ const props = ['foo', 'bar']+ const vnode = renderSlot(+ {+ default: props => {+ receivedProps = props+ return [h('div')]+ },+ },+ 'default',+ props as any,+ undefined,+ undefined,+ 'branch',+ )++ expect(receivedProps).toBe(props)+ expect(receivedProps).not.toHaveProperty('key')+ expect(vnode.key).toBe('branch')+ })++ it('should prefer user-provided slot prop keys over compiler-injected keys', () => {+ let receivedProps: any+ const props = { key: 'user' }+ const vnode = renderSlot(+ {+ default: props => {+ receivedProps = props+ return [h('div')]+ },+ },+ 'default',+ props,+ undefined,+ undefined,+ 'branch',+ )++ expect(receivedProps).toBe(props)+ expect(vnode.key).toBe('user')+ })++ it('should preserve compiler-injected slot keys in custom element mode', () => {+ setCurrentRenderingInstance({ type: {}, ce: {} } as any)++ let vnode = renderSlot({}, 'default', {}, undefined, undefined, 0)+ let slot = (vnode.children as any[])[0]+ expect(slot.type).toBe('slot')+ expect(slot.key).toBe(0)+ expect(vnode.patchFlag).toBe(PatchFlags.BAIL)++ vnode = renderSlot(+ {},+ 'default',+ { key: 'user' },+ undefined,+ undefined,+ 'branch',+ )+ slot = (vnode.children as any[])[0]+ expect(slot.key).toBe('user')+ })+ it('should render slot fallback', () => { const vnode = renderSlot({}, 'default', { key: 'foo' }, () => ['fallback']) expect(vnode.children).toEqual(['fallback'])packages/runtime-core/src/helpers/renderSlot.tsindex 8f65f8af4..2a68ee3e4 100644@@ -16,7 +16,7 @@ import { isVNode, openBlock, } from '../vnode'-import { PatchFlags, SlotFlags, isSymbol } from '@vue/shared'+import { PatchFlags, SlotFlags, extend, isSymbol } from '@vue/shared' import { warn } from '../warning' import { isAsyncWrapper } from '../apiAsyncComponent' @@ -32,6 +32,7 @@ export function renderSlot( // the compiler and guaranteed to be a function returning an array fallback?: () => VNodeArrayChildren, noSlotted?: boolean,+ branchKey?: PropertyKey, ): VNode { if ( currentRenderingInstance!.ce ||@@ -39,17 +40,21 @@ export function renderSlot( isAsyncWrapper(currentRenderingInstance!.parent) && currentRenderingInstance!.parent.ce) ) {- const hasProps = Object.keys(props).length > 0+ const slotProps =+ branchKey != null && props.key == null+ ? extend({}, props, { key: branchKey })+ : props+ const hasProps = Object.keys(slotProps).length > 0 // in custom element mode, render <slot/> as actual slot outlets // wrap it with a fragment because in shadowRoot: false mode the slot // element gets replaced by injected content- if (name !== 'default') props.name = name+ if (name !== 'default') slotProps.name = name return ( openBlock(), createBlock( Fragment, null,- [createVNode('slot', props, fallback && fallback())],+ [createVNode('slot', slotProps, fallback && fallback())], hasProps ? PatchFlags.BAIL : PatchFlags.STABLE_FRAGMENT, ) )@@ -80,6 +85,7 @@ export function renderSlot( const validSlotContent = slot && ensureValidVNode(slot(props)) const slotKey = props.key ||+ branchKey || // slot content array of a dynamic conditional slot may have a branch // key attached in the `createSlots` helper, respect that (validSlotContent && (validSlotContent as any).key)