Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental "ref sugar": local variables sharing a name with a top-level ref are overwritten #3445

Closed
zhangenming opened this issue Mar 18, 2021 · 11 comments · Fixed by #3449
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. 🐞 bug Something isn't working

Comments

@zhangenming
Copy link
Contributor

zhangenming commented Mar 18, 2021

Version

3.0.7

Reproduction link

https://codesandbox.io/s/eloquent-antonelli-xr3fb?file=/src/App.vue

Steps to reproduce

<template>
  <button @click="test">...</button>
</template>

<script setup>
ref: x = 1
function test() {
  const x = { value: 2 }
  console.log(x) //will  show 1, should show 2
}
</script>

What is expected?

show 2

What is actually happening?

show 1

@LinusBorg
Copy link
Member

reproduction missing, also don't use let

@zhangenming
Copy link
Contributor Author

reproduction missing, also don't use let

so, how reproduction vue setup with jsfiddle

@posva
Copy link
Member

posva commented Mar 18, 2021

Read https://new-issue.vuejs.org/?repo=vuejs/vue-next#why-repro. There are links too

@zhangenming
Copy link
Contributor Author

<template>
  <button @click="test">...</button>
</template>

<script setup>
ref: x = 1
function test() {
  const x = { value: 2 }
  console.log(x) //will  show 1, should show 2
}
</script>

that is all
quite clearly, not necessary at all reproduction ...
scope mistake, compiler has a bug

@LinusBorg
Copy link
Member

LinusBorg commented Mar 18, 2021

You are misunderstanding how ref: works:

ref: x = 1
function test() {
  x = 2
  console.log(x) //will  show 2
}

@jacekkarczmarczyk
Copy link
Contributor

jacekkarczmarczyk commented Mar 18, 2021

@LinusBorg you're using the "outside" x, OP is using another var defined inside the function

const x = 1
function test() {
  const x = 2
  console.log(x)  // 2
}

but

ref: x = 1
function test() {
  const x = 2
  console.log(x)  // 1
}

@zhangenming
Copy link
Contributor Author

zhangenming commented Mar 19, 2021

You are misunderstanding how ref: works:

ref: x = 1
function test() {
  x = 2
  console.log(x) //will  show 2
}

你应该没明白我的意思
我是说
我用ref申请了一个变量x
然后在函数内也申请了一个变量, 名字叫x, 恰巧和外面的ref相同了
那么 编译器就会把函数内部的x 当成ref的x
这意味着 我x是全局的了 内部不能有相同的名字
难道这是正常的?
这应该是一个bug
You may not understand what I mean
I mean
I used ref to apply for a variable x
Then a variable is also applied for in the function, the name is x, which happens to be the same as the ref outside
Then the compiler will treat the x inside the function as the x of ref
This means that ref x is global and cannot have the same name inside a function
Is this normal?
This should be a bug

简单说
如果函数作用域内一个变量名和外部的一个ref变量名相同, 编译器会把他当做外部的ref
in short
If a function scope variable has the same name as an outside ref variable, the compiler will treat it as outside ref

@zhangenming
Copy link
Contributor Author

@LinusBorg you're using the "outside" x, OP is using another var defined inside the function

const x = 1
function test() {
  const x = 2
  console.log(x)  // 2
}

but

ref: x = 1
function test() {
  const x = 2
  console.log(x)  // 1
}

Not exactly correct

ref: x = 1
function test() {
  const x = 2
  console.log(x)  // not show 1, will show undefiend, because the compiler will convert to console.log(x.value)
}

What's worse

ref: Variables_with_the_same_name_in_two_scopes = 1
function test() {
  let Variables_with_the_same_name_in_two_scopes 
  console.log(Variables_with_the_same_name_in_two_scopes)  // will ERROR, console.log(Variables_with_the_same_name_in_two_scopes.value) ,  Cannot read property 'value' of undefined
}

@LinusBorg
Copy link
Member

LinusBorg commented Mar 19, 2021

@jacekkarczmarczyk Thanks. totally missed the point of the example.

Looks like a bug in the compiler for that experimental syntax, indeed.

@zhangenming Sorry for misunderstanding.

@LinusBorg LinusBorg added the 🐞 bug Something isn't working label Mar 19, 2021
@LinusBorg LinusBorg reopened this Mar 19, 2021
@LinusBorg LinusBorg changed the title ref, scope of variable experimental "ref sugar": local variables sharing a name with a top-level ref are overwritten Mar 19, 2021
@zhangenming
Copy link
Contributor Author

zhangenming commented Mar 19, 2021

@edison1105
Does your PR allow for the opposite

ref: x
function f(){
  let $x
  console.log($x) // It compiles to x, which should be $x
}

@edison1105
Copy link
Member

edison1105 commented Mar 19, 2021

@zhangenming yes, see test case in the PR.

@HcySunYang HcySunYang added the 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. label Mar 22, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. 🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants