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

Allow exports from namespaces #1453

Open
wants to merge 207 commits into
base: main
Choose a base branch
from
Open

Allow exports from namespaces #1453

wants to merge 207 commits into from

Conversation

sezna
Copy link
Contributor

@sezna sezna commented Apr 30, 2024

This PR introduces exports -- a namespace can export any symbol it has access to. External packages (consumers of this package) will only have access to explicitly exported items.

Next up will be imports, which will allow for importing of specific items.

How it works

Exports are a type of Item, and they appear inside of namespaces (implicit or explicit). Exports are paths, which are resolved to local item ids in the resolver. They are then bound to the scope of that namespace. After that, the exported item is added to the api surface of the namespace.

When we support external package dependencies, we only want to allow importing of explicitly exported items. At that time, we will add a flag to the items to denote if they originate from an export.

This PR also supports export aliases: export { Foo as Bar };

Copy link

Change in memory usage detected by benchmark.

Memory Report for 2c21d22

Test This Branch On Main Difference
compile core + standard lib 16597793 bytes 16582941 bytes 14852 bytes

Copy link

Benchmark for 2c21d22

Click to view benchmark
Test Base PR %
Array append evaluation 332.6±2.86µs 329.2±2.65µs -1.02%
Array literal evaluation 183.8±1.66µs 183.2±0.72µs -0.33%
Array update evaluation 415.5±1.83µs 406.3±1.50µs -2.21%
Core + Standard library compilation 18.8±0.39ms 19.2±0.44ms +2.13%
Deutsch-Jozsa evaluation 5.1±0.06ms 5.0±0.05ms -1.96%
Large file parity evaluation 34.6±0.83ms 34.1±0.25ms -1.45%
Large input file compilation 12.6±0.43ms 12.8±0.47ms +1.59%
Large input file compilation (interpreter) 48.5±1.74ms 48.5±2.33ms 0.00%
Large nested iteration 33.1±0.16ms 32.2±0.15ms -2.72%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1551.0±33.96µs 1562.2±56.26µs +0.72%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.7±0.07ms 7.7±0.09ms 0.00%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1421.7±68.79µs 1419.6±31.63µs -0.15%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 27.5±0.22ms 27.6±0.33ms +0.36%
Teleport evaluation 90.5±3.59µs 88.2±4.23µs -2.54%

Copy link

Change in memory usage detected by benchmark.

Memory Report for ef02ab3

Test This Branch On Main Difference
compile core + standard lib 16597793 bytes 16582941 bytes 14852 bytes

Copy link

Benchmark for ef02ab3

Click to view benchmark
Test Base PR %
Array append evaluation 335.2±4.07µs 331.5±9.04µs -1.10%
Array literal evaluation 170.9±1.86µs 170.6±0.97µs -0.18%
Array update evaluation 411.1±1.59µs 409.4±9.62µs -0.41%
Core + Standard library compilation 21.8±0.78ms 20.0±0.82ms -8.26%
Deutsch-Jozsa evaluation 5.2±0.06ms 5.0±0.19ms -3.85%
Large file parity evaluation 34.3±0.11ms 34.0±0.35ms -0.87%
Large input file compilation 14.1±0.48ms 13.3±0.50ms -5.67%
Large input file compilation (interpreter) 52.1±1.62ms 50.0±2.25ms -4.03%
Large nested iteration 32.9±0.16ms 32.6±0.90ms -0.91%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1602.9±123.67µs 1574.4±93.15µs -1.78%
Perform Runtime Capabilities Analysis (RCA) on large file sample 8.2±0.10ms 7.8±0.09ms -4.88%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1475.3±141.22µs 1421.8±43.21µs -3.63%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 28.8±0.25ms 28.2±0.97ms -2.08%
Teleport evaluation 91.3±5.01µs 88.8±6.68µs -2.74%

Copy link

Change in memory usage detected by benchmark.

Memory Report for 0e74389

Test This Branch On Main Difference
compile core + standard lib 16597793 bytes 16582941 bytes 14852 bytes

Copy link

Benchmark for 0e74389

Click to view benchmark
Test Base PR %
Array append evaluation 336.8±1.70µs 333.3±2.21µs -1.04%
Array literal evaluation 188.0±8.89µs 170.8±1.09µs -9.15%
Array update evaluation 416.6±1.73µs 410.3±1.26µs -1.51%
Core + Standard library compilation 21.5±1.28ms 22.2±1.35ms +3.26%
Deutsch-Jozsa evaluation 5.2±0.06ms 5.1±0.08ms -1.92%
Large file parity evaluation 34.7±0.17ms 34.2±0.23ms -1.44%
Large input file compilation 14.9±0.67ms 15.0±0.83ms +0.67%
Large input file compilation (interpreter) 52.9±2.90ms 52.6±2.30ms -0.57%
Large nested iteration 33.4±0.90ms 32.9±1.26ms -1.50%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1640.2±160.56µs 1641.3±139.12µs +0.07%
Perform Runtime Capabilities Analysis (RCA) on large file sample 8.4±0.19ms 8.4±0.26ms 0.00%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1505.2±179.90µs 1504.3±194.09µs -0.06%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 29.1±0.46ms 29.2±0.39ms +0.34%
Teleport evaluation 92.1±4.03µs 88.8±5.15µs -3.58%

Copy link

Change in memory usage detected by benchmark.

Memory Report for de39c3f

Test This Branch On Main Difference
compile core + standard lib 16634289 bytes 16619429 bytes 14860 bytes

Copy link

Benchmark for de39c3f

Click to view benchmark
Test Base PR %
Array append evaluation 327.5±2.81µs 327.8±2.80µs +0.09%
Array literal evaluation 171.1±1.60µs 171.1±1.46µs 0.00%
Array update evaluation 406.9±2.38µs 406.9±2.41µs 0.00%
Core + Standard library compilation 23.1±1.14ms 23.0±0.99ms -0.43%
Deutsch-Jozsa evaluation 5.1±0.05ms 5.1±0.04ms 0.00%
Large file parity evaluation 34.3±0.11ms 34.3±0.24ms 0.00%
Large input file compilation 14.5±0.51ms 14.9±0.46ms +2.76%
Large input file compilation (interpreter) 53.4±1.40ms 55.5±1.73ms +3.93%
Large nested iteration 32.5±0.38ms 32.6±0.17ms +0.31%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1661.2±140.82µs 1624.5±154.80µs -2.21%
Perform Runtime Capabilities Analysis (RCA) on large file sample 8.5±0.26ms 8.6±0.20ms +1.18%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1497.5±157.47µs 1480.3±152.03µs -1.15%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 29.2±0.54ms 29.2±0.43ms 0.00%
Teleport evaluation 89.8±4.19µs 89.7±5.72µs -0.11%

Copy link

Change in memory usage detected by benchmark.

Memory Report for a400509

Test This Branch On Main Difference
compile core + standard lib 16634289 bytes 16619429 bytes 14860 bytes

Copy link

Change in memory usage detected by benchmark.

Memory Report for 18711a2

Test This Branch On Main Difference
compile core + standard lib 16634289 bytes 16619429 bytes 14860 bytes

Copy link

Benchmark for 18711a2

Click to view benchmark
Test Base PR %
Array append evaluation 326.5±1.53µs 336.2±3.34µs +2.97%
Array literal evaluation 176.7±1.30µs 174.9±6.39µs -1.02%
Array update evaluation 408.6±3.46µs 414.6±2.13µs +1.47%
Core + Standard library compilation 21.1±0.71ms 20.7±0.96ms -1.90%
Deutsch-Jozsa evaluation 5.0±0.04ms 5.1±0.10ms +2.00%
Large file parity evaluation 34.0±0.16ms 34.2±0.35ms +0.59%
Large input file compilation 13.2±0.21ms 13.2±0.23ms 0.00%
Large input file compilation (interpreter) 49.2±1.00ms 52.5±1.81ms +6.71%
Large nested iteration 32.2±0.15ms 33.1±0.19ms +2.80%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1584.4±72.82µs 1581.2±84.55µs -0.20%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.8±0.17ms 8.1±0.18ms +3.85%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1471.6±155.17µs 1433.7±70.08µs -2.58%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 29.2±0.28ms 28.5±0.29ms -2.40%
Teleport evaluation 87.2±3.76µs 89.0±3.95µs +2.06%

Copy link

Change in memory usage detected by benchmark.

Memory Report for 51c39ce

Test This Branch On Main Difference
compile core + standard lib 16634289 bytes 16619429 bytes 14860 bytes

Copy link

Benchmark for 51c39ce

Click to view benchmark
Test Base PR %
Array append evaluation 327.7±1.49µs 335.6±3.41µs +2.41%
Array literal evaluation 176.0±0.78µs 170.7±2.54µs -3.01%
Array update evaluation 410.7±15.32µs 414.4±4.56µs +0.90%
Core + Standard library compilation 20.6±0.99ms 21.3±0.90ms +3.40%
Deutsch-Jozsa evaluation 5.0±0.05ms 5.3±0.07ms +6.00%
Large file parity evaluation 34.1±0.50ms 34.3±0.52ms +0.59%
Large input file compilation 14.0±1.13ms 13.6±0.60ms -2.86%
Large input file compilation (interpreter) 49.2±0.99ms 51.0±2.08ms +3.66%
Large nested iteration 32.3±0.13ms 33.0±0.35ms +2.17%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1596.6±112.77µs 1617.1±177.52µs +1.28%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.9±0.13ms 8.0±0.15ms +1.27%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1466.6±159.66µs 1469.2±144.13µs +0.18%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 28.4±0.29ms 28.5±0.44ms +0.35%
Teleport evaluation 89.2±4.15µs 93.1±3.94µs +4.37%

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't have to be this PR, but just so I don't forget, we should make a note to update the syntax highlighting grammars (codemirror and TextMate) with the new keywords.

Comment on lines +521 to +522
// bind all exported symbols in a follow-on step
resolver.resolve_exports(package);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the behavior is ok here, but the method name and comment confused me.

Would "resolve and bind all imports and exports" be a more complete description of what's happening here?

#[diagnostic(code("Qsc.Resolve.ExportedNonItem"))]
ExportedNonItem(#[label] Span),

#[error("exporting external items is not yet supported")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to continue my crusade against the word "yet" in errors...

Suggested change
#[error("exporting external items is not yet supported")]
#[error("exporting external items is not supported")]

Comment on lines +125 to +127
#[error("export statements are only allowed in a namespace scope")]
#[diagnostic(code("Qsc.Resolve.ExportFromNonNamespaceScope"))]
ExportFromNonNamespaceScope(Span),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to tweak the error message here, for the user's sake. The word "namespace" won't make sense to them as we don't want them using the namespace keyword anymore, and they won't know what "implicit namespaces" are.

Maybe just state what we don't want to happen, like:

Suggested change
#[error("export statements are only allowed in a namespace scope")]
#[diagnostic(code("Qsc.Resolve.ExportFromNonNamespaceScope"))]
ExportFromNonNamespaceScope(Span),
#[error("export statements are not allowed in a local scope")]
#[diagnostic(code("Qsc.Resolve.ExportFromLocalScope"))]
ExportFromLocalScope(Span),

or

Suggested change
#[error("export statements are only allowed in a namespace scope")]
#[diagnostic(code("Qsc.Resolve.ExportFromNonNamespaceScope"))]
ExportFromNonNamespaceScope(Span),
#[error("export statements are not allowed in a callable body")]
#[diagnostic(code("Qsc.Resolve.ExportFromCallableBody"))]
ExportFromCallableBody(Span),

or maybe something better if you can think of it.

@@ -262,6 +272,8 @@ pub enum ItemKind {
Open(Idents, Option<Box<Ident>>),
/// A `newtype` declaration.
Ty(Box<Ident>, Box<TyDef>),
/// An export declaration
Export(ExportDecl),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we handle common item properties, like doc comments, attributes and visibility modifiers, on export and import items?

/// doc comment!
@Config(Base)
internal import Bar.BarOperation;

I guess the current answer is that they don't cause any errors and have no effect, but is this desirable?

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.

None yet

2 participants