Skip to content

fix(semantic): enter scope after check_type for TSConditionalType#6270

Closed
DonIsaac wants to merge 1 commit intomainfrom
don/10-03-fix_semantic_enter_scope_after_check_type_for_tsconditionaltype
Closed

fix(semantic): enter scope after check_type for TSConditionalType#6270
DonIsaac wants to merge 1 commit intomainfrom
don/10-03-fix_semantic_enter_scope_after_check_type_for_tsconditionaltype

Conversation

@DonIsaac
Copy link
Contributor

@DonIsaac DonIsaac commented Oct 3, 2024

When visiting a TSConditionalType, enter a new scope after visiting the check_type, and exit before entering the false_type.

Fixes this case, where B should have a reference. I can't find the issue that was reported with this, if someone finds it please link it to this PR.

type S<A> = A extends /* enter scope */ (infer B extends number ? string : never)
  ? B 
  : /* exit scope before false branch */ false;

Ideally we'd update ast_tools to let us configure the order of enter/leave scopes. CC: @overlookmotel.

@graphite-app
Copy link
Contributor

graphite-app bot commented Oct 3, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

Copy link
Contributor Author

DonIsaac commented Oct 3, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @DonIsaac and the rest of your teammates on Graphite Graphite

@github-actions github-actions bot added A-linter Area - Linter A-semantic Area - Semantic A-ast Area - AST labels Oct 3, 2024
@DonIsaac DonIsaac added the C-bug Category - Bug label Oct 3, 2024 — with Graphite App
@DonIsaac DonIsaac changed the title fix(semantic): enter scope after check_type for TSConditionalType fix(semantic): enter scope after check_type for TSConditionalType Oct 3, 2024
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 3, 2024

CodSpeed Performance Report

Merging #6270 will not alter performance

Comparing don/10-03-fix_semantic_enter_scope_after_check_type_for_tsconditionaltype (3556148) with main (2f888ed)

Summary

✅ 29 untouched benchmarks

@DonIsaac DonIsaac marked this pull request as ready for review October 3, 2024 15:07
@DonIsaac DonIsaac requested a review from Dunqing October 3, 2024 15:07
@Boshen
Copy link
Member

Boshen commented Oct 3, 2024

The usual drill for this is @Dunqing will dig into tsc to find the matching resolution code. He's on holiday so we'll have to wait a bit.

@DonIsaac DonIsaac force-pushed the don/10-03-fix_semantic_enter_scope_after_check_type_for_tsconditionaltype branch 2 times, most recently from d89beab to 97e607c Compare October 3, 2024 18:46
@DonIsaac DonIsaac force-pushed the don/10-03-fix_semantic_enter_scope_after_check_type_for_tsconditionaltype branch from 97e607c to 3556148 Compare October 3, 2024 18:50
@overlookmotel
Copy link
Member

overlookmotel commented Oct 4, 2024

Ideally we'd update ast_tools to let us configure the order of enter/leave scopes.

You can (and should). Add #[scope(enter_before)] to the extends_type field. Same as it is in TSEnumDeclaration:

pub struct TSEnumDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub members: Vec<'a, TSEnumMember<'a>>,

If you do that, ast_tools will update Visit and VisitMut automatically, and you don't need to add visit_ts_conditional_type method to impl<'a> Visit<'a> for SemanticBuilder<'a>. It'll also make Traverse enter the scope at the right point.

Note: Run just ast to run ast_tools codegen after adding the attribute.

@DonIsaac
Copy link
Contributor Author

DonIsaac commented Oct 4, 2024

Ideally we'd update ast_tools to let us configure the order of enter/leave scopes.

You can (and should). Add #[scope(enter_before)] to the extends_type field. Same as it is in TSEnumDeclaration:

pub struct TSEnumDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub members: Vec<'a, TSEnumMember<'a>>,

If you do that, ast_tools will update Visit and VisitMut automatically, and you don't need to add visit_ts_conditional_type method to impl<'a> Visit<'a> for SemanticBuilder<'a>. It'll also make Traverse enter the scope at the right point.

Note: Run just ast to run ast_tools codegen after adding the attribute.

Is there also an #[scope(exit_after)]?

@overlookmotel
Copy link
Member

Is there also an #[scope(exit_after)]?

No. We haven't found a use for that. But it wouldn't be hard to add if we need it.

@DonIsaac
Copy link
Contributor Author

DonIsaac commented Oct 5, 2024

We need that to exit the scope before the false branch, since inferred type parameters (e.g. infer R) are only in scope within the true branch.

@DonIsaac
Copy link
Contributor Author

DonIsaac commented Oct 7, 2024

Closed in favor of #6351

@DonIsaac DonIsaac closed this Oct 7, 2024
Boshen pushed a commit that referenced this pull request Oct 9, 2024
Fixes the same problem as #6270, but uses `#[scope(enter_before)]` and `#[scope(exit_after)]` to correct scope entry/exit locations.
@Boshen Boshen deleted the don/10-03-fix_semantic_enter_scope_after_check_type_for_tsconditionaltype branch February 17, 2025 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast Area - AST A-linter Area - Linter A-semantic Area - Semantic C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments