diff --git a/src/compiler/compile/nodes/shared/Expression.ts b/src/compiler/compile/nodes/shared/Expression.ts
index d4a219418b..8bfc2bffce 100644
--- a/src/compiler/compile/nodes/shared/Expression.ts
+++ b/src/compiler/compile/nodes/shared/Expression.ts
@@ -78,11 +78,14 @@ export default class Expression {
if (scope.has(name)) return;
- if (name[0] === '$' && template_scope.names.has(name.slice(1))) {
- component.error(node, {
- code: `contextual-store`,
- message: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
- });
+ if (name[0] === '$') {
+ const store_name = name.slice(1);
+ if (template_scope.names.has(store_name) || scope.has(store_name)) {
+ component.error(node, {
+ code: `contextual-store`,
+ message: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
+ });
+ }
}
if (template_scope.is_let(name)) {
diff --git a/test/runtime/samples/store-template-expression-scope/_config.js b/test/runtime/samples/store-template-expression-scope/_config.js
new file mode 100644
index 0000000000..e2d6fc7808
--- /dev/null
+++ b/test/runtime/samples/store-template-expression-scope/_config.js
@@ -0,0 +1,3 @@
+export default {
+ error: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
+};
diff --git a/test/runtime/samples/store-template-expression-scope/main.svelte b/test/runtime/samples/store-template-expression-scope/main.svelte
new file mode 100644
index 0000000000..1c9ccb2933
--- /dev/null
+++ b/test/runtime/samples/store-template-expression-scope/main.svelte
@@ -0,0 +1,9 @@
+
+
+