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

[bug][scope analysis] A symbol has two owner scopes in the same time #2205

Closed
hyf0 opened this issue Jan 29, 2024 · 11 comments
Closed

[bug][scope analysis] A symbol has two owner scopes in the same time #2205

hyf0 opened this issue Jan 29, 2024 · 11 comments
Labels
A-semantic Area - Semantic C-bug Category - Bug

Comments

@hyf0
Copy link
Contributor

hyf0 commented Jan 29, 2024

(() => {
	function a() {
		b()
	}
	{
		var b = () => {}
	}
	a()
})()

I notice that symbol b belong to two owner scopes after the sematic analysis in [email protected].

@Dunqing Dunqing added C-bug Category - Bug A-semantic Area - Semantic labels Jan 29, 2024
@Boshen Boshen changed the title [bug][scope analysis] A symbol has two onwer scopes in the same time [bug][scope analysis] A symbol has two owner scopes in the same time Jan 29, 2024
@Dunqing
Copy link
Member

Dunqing commented Jan 29, 2024

I put the code in the playground

@Boshen
Copy link
Member

Boshen commented Jan 29, 2024

Scope1 (ScopeFlags(StrictMode | Top)) {
  Scope2 (ScopeFlags(StrictMode | Function | Arrow)) {
    Bindings: {
      a (SymbolFlags(FunctionScopedVariable))
      b (SymbolFlags(FunctionScopedVariable))
    }
    Scope3 (ScopeFlags(StrictMode | Function)) {
    }
    Scope4 (ScopeFlags(StrictMode)) {
      Bindings: {
        b (SymbolFlags(FunctionScopedVariable))
      }
      Scope5 (ScopeFlags(StrictMode | Function | Arrow)) {
      }
    }
  }
}

The logic here is that these scopes have the variable b. There is no owning logic.

@hyf0
Copy link
Contributor Author

hyf0 commented Jan 29, 2024

The logic here is that these scopes have the variable b. There is no owning logic.

So, is this logic expected? A binding could belong to many scopes in the same time.

If this is expected, what's the right way to get a binding's owner scope(the scope that store it in the first place)?

@hyf0
Copy link
Contributor Author

hyf0 commented Jan 29, 2024

I put the code in the playground

Wow I didn't thought the playgound is already support visulize the scopes. This is very useful.

If the playgound could aslo print out the symbol id for symbols, it will be very useful to distinguash symbols that has the same name.

@hyf0
Copy link
Contributor Author

hyf0 commented Jan 29, 2024

The scope output for input

(() => {
        var b;
	function a() {
		b()
	}
	{
		var b = () => {}
	}
	a()
})()

is

Scope1 (ScopeFlags(StrictMode | Top)) {
  Scope2 (ScopeFlags(StrictMode | Function | Arrow)) {
    Bindings: {
      b (SymbolFlags(FunctionScopedVariable))
      a (SymbolFlags(FunctionScopedVariable))
    }
    Scope3 (ScopeFlags(StrictMode | Function)) {
    }
    Scope4 (ScopeFlags(StrictMode)) {
      Scope5 (ScopeFlags(StrictMode | Function | Arrow)) {
      }
    }
  }
}

I'm not sure if this is right or wrong, but it does feel weird to me.

@Dunqing
Copy link
Member

Dunqing commented Jan 29, 2024

If the playgound could aslo print out the symbol id for symbols, it will be very useful to distinguash symbols that has the same name.

The symbol view may be useful for you. When you hover over the span, the b symbol has been highlightedimage

@Dunqing
Copy link
Member

Dunqing commented Jan 29, 2024

If this is expected, what's the right way to get a binding's owner scope(the scope that store it in the first place)?

I think we can't get the binding's owner scope now.

Although the b symbol contains the symbol in both its scope and its parent scope, that symbol id and node id both point to the same one, I don't know if this will help you

@hyf0
Copy link
Contributor Author

hyf0 commented Jan 29, 2024

If this is expected, what's the right way to get a binding's owner scope(the scope that store it in the first place)?

I think we can't get the binding's owner scope now.

Although the b symbol contains the symbol in both its scope and its parent scope, that symbol id and node id both point to the same one, I don't know if this will help you

Yeah, I know that. I just want to know if the current behavior is correct. If it's expected, I will try to solve my problems in user land. If it's not, it becomes a bug that oxc needs to fix.

@Dunqing
Copy link
Member

Dunqing commented Jan 29, 2024

I think that is expected. But need @Boshen to confirm this.

@Boshen
Copy link
Member

Boshen commented Jan 30, 2024

@hyf0 Let me investigate and get back to you. var hoisting logic is complicated :-)

@Dunqing
Copy link
Member

Dunqing commented Jan 30, 2024

After the communication, we think the result is expected.

@Dunqing Dunqing closed this as completed Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-semantic Area - Semantic C-bug Category - Bug
Projects
None yet
Development

No branches or pull requests

3 participants