Repository: sveltejs/svelte (JavaScript)
Date: 2020-03-16 16:02:48+00:00
Task type: Bug Fix

Issue:
Compiler error in 3.20.0 with certain slot usage
**Describe the bug**
As of 3.20.0, the compiler is now throwing an exception when compiling certain slot usages.

**Logs**
Compile time exception: `Variable 't' already initialised with a different value`

**To Reproduce**
```svelte
<slot>
	<div title={foo}>foo</div>
	<div>bar</div>
</slot>
```

**Expected behavior**
This compiles without an exception.

**Stacktraces**
<details>
  <summary>Stack trace</summary>

```
Error: Variable 't' already initialised with a different value
    at Block$1.add_variable (.../src/compiler/compile/render_dom/Block.ts:210:10)
    at Block$1.add_element (.../src/compiler/compile/render_dom/Block.ts:177:8)
    at TextWrapper.render (.../src/compiler/compile/render_dom/wrappers/Text.ts:47:9)
    at FragmentWrapper.render (.../src/compiler/compile/render_dom/wrappers/Fragment.ts:147:18)
    at SlotWrapper.render (.../src/compiler/compile/render_dom/wrappers/Slot.ts:117:18)        
    at FragmentWrapper.render (.../src/compiler/compile/render_dom/wrappers/Fragment.ts:147:18)
    at new Renderer (.../src/compiler/compile/render_dom/Renderer.ts:101:17)
    at dom (.../src/compiler/compile/render_dom/index.ts:17:19)
    at Object.render_dom [as compile] (.../src/compiler/compile/index.ts:97:6)
```
</details>

**Information about your Svelte project:**
- Svelte 3.20.0
- REPL

**Severity**
Unknown.

**Additional context**
Mentioned in Discord [here](https://discordapp.com/channels/457912077277855764/457912077277855766/689127606179725543).

Compiler error in 3.20.0 with certain slot usage
**Describe the bug**
As of 3.20.0, the compiler is now throwing an exception when compiling certain slot usages.

**Logs**
Compile time exception: `Variable 't' already initialised with a different value`

**To Reproduce**
```svelte
<slot>
	<div title={foo}>foo</div>
	<div>bar</div>
</slot>
```

**Expected behavior**
This compiles without an exception.

**Stacktraces**
<details>
  <summary>Stack trace</summary>

```
Error: Variable 't' already initialised with a different value
    at Block$1.add_variable (.../src/compiler/compile/render_dom/Block.ts:210:10)
    at Block$1.add_element (.../src/compiler/compile/render_dom/Block.ts:177:8)
    at TextWrapper.render (.../src/compiler/compile/render_dom/wrappers/Text.ts:47:9)
    at FragmentWrapper.render (.../src/compiler/compile/render_dom/wrappers/Fragment.ts:147:18)
    at SlotWrapper.render (.../src/compiler/compile/render_dom/wrappers/Slot.ts:117:18)        
    at FragmentWrapper.render (.../src/compiler/compile/render_dom/wrappers/Fragment.ts:147:18)
    at new Renderer (.../src/compiler/compile/render_dom/Renderer.ts:101:17)
    at dom (.../src/compiler/compile/render_dom/index.ts:17:19)
    at Object.render_dom [as compile] (.../src/compiler/compile/index.ts:97:6)
```
</details>

**Information about your Svelte project:**
- Svelte 3.20.0
- REPL

**Severity**
Unknown.

**Additional context**
Mentioned in Discord [here](https://discordapp.com/channels/457912077277855764/457912077277855766/689127606179725543).



Issue discussion:
I spent the day troubleshooting why my apps won't start.

Any slot where the first element is a ``<!-- comment -->`` halts the build.

``<slot>foobar<!-- placeholder--></slot>`` **works**
``<slot> <!-- placeholder--></slot>`` **works**
``<slot></slot>`` **works**
``<slot/>`` **works**
``<slot><!-- placeholder--></slot>`` **doesn't work**
``<slot><!-- placeholder--> </slot>`` **doesn't work**
``<slot><!-- placeholder--> foobar </slot>`` **doesn'tworks**
I spent the day troubleshooting why my apps won't start.

Any slot where the first element is a ``<!-- comment -->`` halts the build.

``<slot>foobar<!-- placeholder--></slot>`` **works**
``<slot> <!-- placeholder--></slot>`` **works**
``<slot></slot>`` **works**
``<slot/>`` **works**
``<slot><!-- placeholder--></slot>`` **doesn't work**
``<slot><!-- placeholder--> </slot>`` **doesn't work**
``<slot><!-- placeholder--> foobar </slot>`` **doesn'tworks**

Solution patch:
diff --git a/src/compiler/compile/render_dom/wrappers/Slot.ts b/src/compiler/compile/render_dom/wrappers/Slot.ts
--- a/src/compiler/compile/render_dom/wrappers/Slot.ts
+++ b/src/compiler/compile/render_dom/wrappers/Slot.ts
@@ -38,6 +38,7 @@ export default class SlotWrapper extends Wrapper {
 				name: this.renderer.component.get_unique_name(`fallback_block`),
 				type: 'fallback'
 			});
+			renderer.blocks.push(this.fallback);
 		}
 
 		this.fragment = new FragmentWrapper(
@@ -115,7 +116,6 @@ export default class SlotWrapper extends Wrapper {
 
 		if (this.fallback) {
 			this.fragment.render(this.fallback, null, x`#nodes` as Identifier);
-			renderer.blocks.push(this.fallback);
 		}
 
 		const slot = block.get_unique_name(`${sanitize(slot_name)}_slot`);


Test patch:
diff --git a/test/runtime/samples/component-slot-fallback-3/Inner.svelte b/test/runtime/samples/component-slot-fallback-3/Inner.svelte
new file mode 100644
--- /dev/null
+++ b/test/runtime/samples/component-slot-fallback-3/Inner.svelte
@@ -0,0 +1,6 @@
+<slot>
+	<div>Hello</div>
+	<div>world</div>
+	<div>Bye</div>
+	<div>World</div>
+</slot>
diff --git a/test/runtime/samples/component-slot-fallback-3/_config.js b/test/runtime/samples/component-slot-fallback-3/_config.js
new file mode 100644
--- /dev/null
+++ b/test/runtime/samples/component-slot-fallback-3/_config.js
@@ -0,0 +1,6 @@
+export default {
+	html: `
+	<div>Hello World</div>
+	<div>Hello</div><div>world</div><div>Bye</div><div>World</div>
+	`,
+};
diff --git a/test/runtime/samples/component-slot-fallback-3/main.svelte b/test/runtime/samples/component-slot-fallback-3/main.svelte
new file mode 100644
--- /dev/null
+++ b/test/runtime/samples/component-slot-fallback-3/main.svelte
@@ -0,0 +1,9 @@
+<script>
+	import Inner from "./Inner.svelte";
+</script>
+
+<Inner>
+	<div>Hello World</div>
+</Inner>
+
+<Inner></Inner>