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

Refactor resolution to use hierarchical namespaces #1312

Closed
wants to merge 80 commits into from
Closed

Conversation

sezna
Copy link
Contributor

@sezna sezna commented Mar 26, 2024

This PR introduces a feature called hierarchical namespaces, a module-like system that maintains backward compatibility. It is the foundational groundwork that will pave the way for:

  1. Naked namespaces (files without namespace declarations)
  2. Package publishing and sharing
  3. Explicit item imports
  4. Exports and re-exports
  5. and the rest of the stuff here

Key changes include:

  1. VecIdent type introduction: This abstraction over Vec<Ident> represents the new namespaces in the AST. Alternate names could be DotIdent or Path.
  2. NamespaceId usage: This replaces Rc<str> for namespace tracking.
  3. NamespaceTreeRoot introduction: This tracks the namespace hierarchy.
  4. Name resolution algorithm update: The algorithm now checks locals, resolves the namespace part of the symbol in the NamespaceTreeRoot, and resolves the symbol in the candidate namespaces and scopes, following our shadowing rules and compile error hints.

This PR maintains 100% backward compatibility with the existing resolution engine, preserving features like re-opening namespaces, shadowing semantics, merging namespaces via aliases, and absolute-path opens. It also introduces the ability to reference a function in a different namespace if its parent namespace is opened.

Additional notes:

  1. Katas used a form of shadowing that wasn't tested in our compiler. This PR adds a test for that.
  2. Pursuing Use debug printing for expect_test in the parser and other places. #1321 could help differentiate user-facing and compiler-engineer-facing string representations of structs, which are increasingly conflated.
  3. There's potential for refactoring ast, fir, and hir to use shared generic types, reducing duplicate Ident/etc types.
  4. Completions functionality remains the same, but it's not yet aware of the ability to partially open namespaces (e.g. open Microsoft.Quantum. This will be addressed in future updates.

compiler/qsc_frontend/src/resolve.rs Fixed Show fixed Hide fixed
compiler/qsc_hir/src/global.rs Fixed Show fixed Hide fixed
Copy link

github-actions bot commented Apr 4, 2024

Benchmark for b6f90b0

Click to view benchmark
Test Base PR %
Array append evaluation 332.2±3.17µs 339.6±2.71µs +2.23%
Array literal evaluation 177.2±3.48µs 175.9±5.20µs -0.73%
Array update evaluation 414.4±2.26µs 419.3±1.75µs +1.18%
Core + Standard library compilation 18.7±0.72ms 25.0±0.84ms +33.69%
Deutsch-Jozsa evaluation 5.0±0.05ms 5.0±0.06ms 0.00%
Large file parity evaluation 33.7±0.12ms 34.4±0.21ms +2.08%
Large input file compilation 12.0±0.48ms 14.7±0.33ms +22.50%
Large input file compilation (interpreter) 46.8±1.83ms 58.4±3.35ms +24.79%
Large nested iteration 32.5±0.34ms 33.0±0.20ms +1.54%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1543.5±71.51µs 1533.4±86.84µs -0.65%
Perform Runtime Capabilities Analysis (RCA) on large file sample 8.2±0.16ms 8.0±0.20ms -2.44%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1464.3±123.79µs 1448.8±80.40µs -1.06%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.6±0.40ms 21.7±0.41ms +0.46%
Teleport evaluation 80.9±3.45µs 81.1±4.43µs +0.25%

Copy link

github-actions bot commented Apr 4, 2024

Change in memory usage detected by benchmark.

Memory Report for b7d20e7

Test This Branch On Main Difference
compile core + standard lib 16677573 bytes 15969577 bytes 707996 bytes

Copy link

github-actions bot commented Apr 4, 2024

Benchmark for b7d20e7

Click to view benchmark
Test Base PR %
Array append evaluation 343.3±5.61µs 350.9±3.50µs +2.21%
Array literal evaluation 196.4±3.64µs 191.5±3.89µs -2.49%
Array update evaluation 424.0±2.30µs 433.3±8.83µs +2.19%
Core + Standard library compilation 16.8±0.21ms 24.3±1.13ms +44.64%
Deutsch-Jozsa evaluation 5.1±0.15ms 5.1±0.09ms 0.00%
Large file parity evaluation 33.9±0.19ms 34.3±0.22ms +1.18%
Large input file compilation 11.4±0.39ms 14.9±0.26ms +30.70%
Large input file compilation (interpreter) 46.5±2.21ms 58.4±3.57ms +25.59%
Large nested iteration 33.4±0.31ms 34.1±0.18ms +2.10%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1492.3±30.68µs 1508.7±39.62µs +1.10%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.8±0.14ms 7.7±0.09ms -1.28%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1407.3±29.83µs 1422.0±51.95µs +1.04%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.2±0.45ms 21.5±0.37ms +1.42%
Teleport evaluation 80.5±3.50µs 80.9±4.03µs +0.50%

Copy link

github-actions bot commented Apr 4, 2024

Change in memory usage detected by benchmark.

Memory Report for 8609589

Test This Branch On Main Difference
compile core + standard lib 16677573 bytes 15969577 bytes 707996 bytes

Copy link

github-actions bot commented Apr 4, 2024

Benchmark for 8609589

Click to view benchmark
Test Base PR %
Array append evaluation 349.3±2.68µs 338.0±2.86µs -3.24%
Array literal evaluation 192.9±2.87µs 174.6±1.54µs -9.49%
Array update evaluation 431.8±4.77µs 419.3±3.28µs -2.89%
Core + Standard library compilation 17.0±0.36ms 24.5±0.91ms +44.12%
Deutsch-Jozsa evaluation 5.1±0.11ms 5.1±0.05ms 0.00%
Large file parity evaluation 33.7±0.12ms 34.2±0.58ms +1.48%
Large input file compilation 11.8±0.67ms 15.5±0.59ms +31.36%
Large input file compilation (interpreter) 48.2±2.59ms 61.0±3.83ms +26.56%
Large nested iteration 34.2±0.28ms 33.0±0.33ms -3.51%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1509.5±43.86µs 1562.0±181.25µs +3.48%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.9±0.16ms 7.9±0.23ms 0.00%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1433.1±83.60µs 1426.0±55.61µs -0.50%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.3±0.49ms 21.9±0.43ms +2.82%
Teleport evaluation 81.2±4.04µs 81.9±3.89µs +0.86%

Copy link

github-actions bot commented Apr 4, 2024

Change in memory usage detected by benchmark.

Memory Report for 40aaf8c

Test This Branch On Main Difference
compile core + standard lib 16677573 bytes 15969577 bytes 707996 bytes

Copy link

github-actions bot commented Apr 4, 2024

Benchmark for 40aaf8c

Click to view benchmark
Test Base PR %
Array append evaluation 331.4±1.84µs 351.5±2.34µs +6.07%
Array literal evaluation 176.6±1.26µs 189.7±2.55µs +7.42%
Array update evaluation 411.6±2.01µs 430.0±5.10µs +4.47%
Core + Standard library compilation 18.4±1.23ms 24.7±0.85ms +34.24%
Deutsch-Jozsa evaluation 5.1±0.04ms 5.0±0.05ms -1.96%
Large file parity evaluation 33.7±0.14ms 34.5±0.59ms +2.37%
Large input file compilation 12.3±0.55ms 15.7±0.65ms +27.64%
Large input file compilation (interpreter) 47.5±1.86ms 60.2±3.22ms +26.74%
Large nested iteration 32.3±0.75ms 34.1±0.26ms +5.57%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1542.5±126.23µs 1542.6±113.28µs +0.01%
Perform Runtime Capabilities Analysis (RCA) on large file sample 8.0±0.20ms 7.9±0.17ms -1.25%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1456.4±130.44µs 1435.9±36.91µs -1.41%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.6±0.35ms 21.8±0.40ms +0.93%
Teleport evaluation 80.5±3.54µs 81.5±5.06µs +1.24%

Copy link

github-actions bot commented Apr 5, 2024

Change in memory usage detected by benchmark.

Memory Report for a2d299a

Test This Branch On Main Difference
compile core + standard lib 16677573 bytes 15969577 bytes 707996 bytes

Copy link

github-actions bot commented Apr 5, 2024

Benchmark for a2d299a

Click to view benchmark
Test Base PR %
Array append evaluation 352.6±2.85µs 351.4±14.33µs -0.34%
Array literal evaluation 201.7±1.97µs 189.3±0.71µs -6.15%
Array update evaluation 434.9±2.52µs 432.0±16.46µs -0.67%
Core + Standard library compilation 18.1±0.94ms 24.0±0.60ms +32.60%
Deutsch-Jozsa evaluation 5.0±0.03ms 5.1±0.06ms +2.00%
Large file parity evaluation 33.7±0.12ms 34.2±0.31ms +1.48%
Large input file compilation 12.1±0.33ms 14.8±0.22ms +22.31%
Large input file compilation (interpreter) 47.5±1.55ms 58.2±3.24ms +22.53%
Large nested iteration 34.3±0.38ms 33.7±0.20ms -1.75%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1486.7±30.03µs 1504.7±47.18µs +1.21%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.7±0.10ms 7.8±0.09ms +1.30%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1400.9±42.01µs 1415.6±30.25µs +1.05%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.7±0.29ms 21.3±0.36ms -1.84%
Teleport evaluation 80.4±7.60µs 82.5±4.41µs +2.61%

Copy link

github-actions bot commented Apr 5, 2024

Change in memory usage detected by benchmark.

Memory Report for 26a4f5d

Test This Branch On Main Difference
compile core + standard lib 16677893 bytes 15969577 bytes 708316 bytes

Copy link

github-actions bot commented Apr 5, 2024

Change in memory usage detected by benchmark.

Memory Report for 579157b

Test This Branch On Main Difference
compile core + standard lib 16677893 bytes 15969577 bytes 708316 bytes

Copy link

github-actions bot commented Apr 5, 2024

Change in memory usage detected by benchmark.

Memory Report for 7bb5da8

Test This Branch On Main Difference
compile core + standard lib 16677893 bytes 15969577 bytes 708316 bytes

Copy link

github-actions bot commented Apr 5, 2024

Benchmark for 7bb5da8

Click to view benchmark
Test Base PR %
Array append evaluation 344.0±21.95µs 371.5±2.48µs +7.99%
Array literal evaluation 174.7±2.41µs 190.8±3.81µs +9.22%
Array update evaluation 432.4±30.84µs 453.9±6.53µs +4.97%
Core + Standard library compilation 17.5±0.41ms 26.2±0.84ms +49.71%
Deutsch-Jozsa evaluation 5.0±0.05ms 5.2±0.05ms +4.00%
Large file parity evaluation 33.9±0.16ms 34.4±0.39ms +1.47%
Large input file compilation 11.4±0.32ms 16.3±1.18ms +42.98%
Large input file compilation (interpreter) 44.9±1.63ms 61.1±1.91ms +36.08%
Large nested iteration 33.0±0.60ms 36.4±0.59ms +10.30%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1504.9±79.60µs 1496.7±40.94µs -0.54%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.8±0.27ms 7.8±0.19ms 0.00%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1414.4±50.34µs 1432.0±75.00µs +1.24%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.8±0.33ms 21.7±0.62ms -0.46%
Teleport evaluation 81.5±8.31µs 84.5±3.99µs +3.68%

Copy link

github-actions bot commented Apr 6, 2024

Change in memory usage detected by benchmark.

Memory Report for 4e9d46a

Test This Branch On Main Difference
compile core + standard lib 16677893 bytes 15969577 bytes 708316 bytes

Copy link

github-actions bot commented Apr 6, 2024

Benchmark for 4e9d46a

Click to view benchmark
Test Base PR %
Array append evaluation 335.6±6.94µs 349.8±3.50µs +4.23%
Array literal evaluation 170.4±5.18µs 186.0±3.75µs +9.15%
Array update evaluation 415.3±8.45µs 427.2±5.82µs +2.87%
Core + Standard library compilation 17.5±0.55ms 24.2±0.82ms +38.29%
Deutsch-Jozsa evaluation 4.9±0.12ms 5.0±0.14ms +2.04%
Large file parity evaluation 33.1±0.69ms 34.3±0.55ms +3.63%
Large input file compilation 11.6±0.28ms 15.1±0.30ms +30.17%
Large input file compilation (interpreter) 47.0±1.37ms 58.3±1.81ms +24.04%
Large nested iteration 32.6±0.61ms 33.1±0.75ms +1.53%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1464.3±51.31µs 1464.8±64.65µs +0.03%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.7±0.17ms 7.6±0.16ms -1.30%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1383.0±58.56µs 1387.1±55.88µs +0.30%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.2±0.50ms 21.0±0.48ms -0.94%
Teleport evaluation 79.4±5.28µs 80.3±4.54µs +1.13%

Copy link

github-actions bot commented Apr 6, 2024

Change in memory usage detected by benchmark.

Memory Report for 426d697

Test This Branch On Main Difference
compile core + standard lib 16677893 bytes 15969577 bytes 708316 bytes

@sezna sezna closed this Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant