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

Migrate core library to the new Stdlib API/projects system #1919

Merged
merged 34 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9af6f07
convert arrays.qs to the new stdlib
sezna Sep 9, 2024
dd55538
move Convert to modern stdlib
sezna Sep 9, 2024
d3905ae
move diagnostics to new stdlib
sezna Sep 9, 2024
a86106c
convert some more to the modern style
sezna Sep 9, 2024
baf489a
forgotten adds
sezna Sep 9, 2024
9bd4487
wip: refactor stdlib
sezna Sep 9, 2024
50e49a6
port intrinsic and internals to new project format
sezna Sep 10, 2024
84258ab
migrate half of unstable lib
sezna Sep 10, 2024
06b372c
everything migrated except canon and core
sezna Sep 10, 2024
6cd0124
finalize new stdlib
sezna Sep 10, 2024
920dd71
migrate canon and undo unstable migration
sezna Sep 10, 2024
f878951
remove opens
sezna Sep 10, 2024
59bff76
update tests
sezna Sep 10, 2024
22b6ffc
clippy
sezna Sep 10, 2024
e6de5af
fix basics.js test
sezna Sep 10, 2024
0e7f0f7
update integration test
sezna Sep 11, 2024
f73924d
add error for clobbered namespaces
sezna Sep 11, 2024
3019333
Update Core to Range
sezna Sep 11, 2024
456f678
format
sezna Sep 11, 2024
5d04158
i think this works
sezna Sep 11, 2024
0eedaf7
wip: RCA broken
sezna Sep 11, 2024
8ac2f6f
wip
sezna Sep 12, 2024
8c02be8
unwraps that I will remove later -- good luck cesar
sezna Sep 12, 2024
923d6d2
RCA exports fix (#1918)
cesarzc Sep 16, 2024
f05e7cd
merge
sezna Sep 17, 2024
16f3ea3
Merge branch 'main' of github.com:microsoft/qsharp into alex/1911
sezna Sep 24, 2024
b737bb6
fix clippy stuff
sezna Sep 24, 2024
8a4bd39
format qsharp code
sezna Sep 24, 2024
1482ffb
Fix expect i think?
sezna Sep 24, 2024
a8b1638
update docs tests
sezna Sep 24, 2024
5520e2a
fix subtle bug in ns tree construction
sezna Sep 24, 2024
3ebf0ee
update all expects
sezna Sep 24, 2024
3aeefdb
Remove debug prints
sezna Sep 25, 2024
e92609f
improve error message
sezna Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions compiler/qsc_data_structures/src/namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ mod tests;
use rustc_hash::{FxHashMap, FxHashSet};
use std::{cell::RefCell, collections::BTreeMap, fmt::Display, iter::Peekable, ops::Deref, rc::Rc};

pub const PRELUDE: &[&[&str]; 4] = &[
&["Std", "Canon"],
&["Microsoft", "Quantum", "Core"],
&["Std", "Intrinsic"],
&["Std", "Measurement"],
pub const PRELUDE: [[&str; 2]; 5] = [
["Std", "Canon"],
["Std", "Core"],
["Std", "Intrinsic"],
["Std", "Measurement"],
["Std", "Range"],
];

/// An ID that corresponds to a namespace in the global scope.
Expand Down Expand Up @@ -63,6 +64,13 @@ type NamespaceTreeCell = Rc<RefCell<NamespaceTreeNode>>;
/// An entry in the memoization table for namespace ID lookups.
type MemoEntry = (Vec<Rc<str>>, NamespaceTreeCell);

/// Denotes that a namespace from an external package has been overridden by a local package namespace.
/// This renders the contents of the foreign namespace unaccessible.
/// E.g. a user explicitly creating a namespace called `namespace Std.Diagnostics`, where `Std` is the
/// standard library.
#[derive(Debug)]
pub struct ClobberedNamespace;

/// The root of the data structure that represents the namespaces in a program.
/// The tree is a trie (prefix tree) where each node is a namespace and the children are the sub-namespaces.
/// For example, the namespace `Microsoft.Quantum.Canon` would be represented as a traversal from the root node:
Expand Down Expand Up @@ -170,14 +178,22 @@ impl NamespaceTreeRoot {
parent: Option<NamespaceId>,
new_child: NamespaceId,
alias: &str,
) {
) -> Result<(), ClobberedNamespace> {
let parent = parent.unwrap_or_else(|| self.root_id());
let (_, parent_node) = self.find_namespace_by_id(&parent);
let (_, existing_ns) = self.find_namespace_by_id(&new_child);
if let Some(val) = parent_node.borrow().children.get(&Rc::from(alias)) {
if val.borrow().id != existing_ns.borrow().id {
return Err(ClobberedNamespace);
}
}

parent_node
.borrow_mut()
.children
.insert(Rc::from(alias), existing_ns);

Ok(())
}

/// Inserts (or finds) a new namespace as a child of an existing namespace.
Expand All @@ -204,9 +220,9 @@ impl NamespaceTreeRoot {
mut ns: Vec<Rc<str>>,
root: NamespaceId,
base_id: NamespaceId,
) {
) -> Result<(), ClobberedNamespace> {
if ns.is_empty() {
return;
return Ok(());
}
let (_root_name, root_contents) = self.find_namespace_by_id(&root);
// split `ns` into [0..len - 1] and [len - 1]
Expand All @@ -215,15 +231,16 @@ impl NamespaceTreeRoot {

// if the prefix is empty, we are inserting into the root
if prefix.is_empty() {
self.insert_with_id(Some(root), base_id, &suffix);
self.insert_with_id(Some(root), base_id, &suffix)?;
} else {
let prefix_id = root_contents
.borrow_mut()
.insert_or_find_namespace(prefix.into_iter().peekable(), &mut self.assigner)
.expect("empty name checked for above");

self.insert_with_id(Some(prefix_id), base_id, &suffix);
self.insert_with_id(Some(prefix_id), base_id, &suffix)?;
}
Ok(())
}

/// Each item in this iterator is the same, single namespace. The reason there are multiple paths for it,
Expand Down Expand Up @@ -270,20 +287,14 @@ impl IntoIterator for &NamespaceTreeRoot {

impl Default for NamespaceTreeRoot {
fn default() -> Self {
let mut tree = Self {
Self {
assigner: 0,
tree: Rc::new(RefCell::new(NamespaceTreeNode {
children: FxHashMap::default(),
id: NamespaceId::new(0),
})),
memo: RefCell::new(FxHashMap::default()),
};
// insert the prelude namespaces using the `NamespaceTreeRoot` API
for ns in PRELUDE {
let iter = ns.iter().map(|s| Rc::from(*s)).peekable();
let _ = tree.insert_or_find_namespace(iter);
}
tree
}
}

Expand Down
154 changes: 40 additions & 114 deletions compiler/qsc_data_structures/src/namespaces/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,25 @@ fn test_tree_construction() {
NamespaceTreeRoot

children: [
Std(id 1) {
ns1(id 5) {
children: [
Measurement(id 7) {empty node},
Canon(id 2) {empty node},
Intrinsic(id 6) {empty node},
nsc(id 8) {empty node},
nsb(id 7) {empty node},
nsa(id 6) {empty node},
]
},
ns1(id 12) {
ns0(id 1) {
children: [
nsc(id 15) {empty node},
nsb(id 14) {empty node},
nsa(id 13) {empty node},
nsc(id 4) {empty node},
nsb(id 3) {empty node},
nsa(id 2) {empty node},
]
},
ns0(id 8) {
ns2(id 9) {
children: [
nsc(id 11) {empty node},
nsb(id 10) {empty node},
nsa(id 9) {empty node},
]
},
Microsoft(id 3) {
children: [
Quantum(id 4) {
children: [
Core(id 5) {empty node},
]
},
]
},
ns2(id 16) {
children: [
nsc(id 19) {empty node},
nsb(id 18) {empty node},
nsa(id 17) {empty node},
nsc(id 12) {empty node},
nsb(id 11) {empty node},
nsa(id 10) {empty node},
]
},
]
Expand Down Expand Up @@ -121,9 +105,9 @@ fn test_find_id() {
RefCell {
value:
children: [
nsc(id 11) {empty node},
nsb(id 10) {empty node},
nsa(id 9) {empty node},
nsc(id 4) {empty node},
nsb(id 3) {empty node},
nsa(id 2) {empty node},
]
},
},
Expand Down Expand Up @@ -162,9 +146,9 @@ fn test_find_id() {
RefCell {
value:
children: [
nsc(id 15) {empty node},
nsb(id 14) {empty node},
nsa(id 13) {empty node},
nsc(id 8) {empty node},
nsb(id 7) {empty node},
nsa(id 6) {empty node},
]
},
},
Expand Down Expand Up @@ -203,9 +187,9 @@ fn test_find_id() {
RefCell {
value:
children: [
nsc(id 19) {empty node},
nsb(id 18) {empty node},
nsa(id 17) {empty node},
nsc(id 12) {empty node},
nsb(id 11) {empty node},
nsa(id 10) {empty node},
]
},
},
Expand Down Expand Up @@ -234,15 +218,15 @@ fn test_insert_or_find_namespace() {
assert_eq!(ids_sorted, ids);
expect![[r#"
[
9,
2,
3,
4,
6,
7,
8,
10,
11,
13,
14,
15,
17,
18,
19,
12,
]
"#]]
.assert_debug_eq(&ids);
Expand Down Expand Up @@ -271,62 +255,62 @@ fn test_get_namespace_id() {
[
Some(
NamespaceId(
9,
2,
),
),
Some(
NamespaceId(
10,
3,
),
),
Some(
NamespaceId(
11,
4,
),
),
Some(
NamespaceId(
8,
1,
),
),
Some(
NamespaceId(
13,
6,
),
),
Some(
NamespaceId(
14,
7,
),
),
Some(
NamespaceId(
15,
8,
),
),
Some(
NamespaceId(
12,
5,
),
),
Some(
NamespaceId(
17,
10,
),
),
Some(
NamespaceId(
18,
11,
),
),
Some(
NamespaceId(
19,
12,
),
),
Some(
NamespaceId(
16,
9,
),
),
]
Expand All @@ -352,64 +336,6 @@ fn test_tree_iter() {
[
[],
],
[
[
"Std",
],
],
[
[
"Std",
"Canon",
],
[
"Std",
"Canon",
],
],
[
[
"Microsoft",
],
],
[
[
"Microsoft",
"Quantum",
],
],
[
[
"Microsoft",
"Quantum",
"Core",
],
[
"Microsoft",
"Quantum",
"Core",
],
],
[
[
"Std",
"Intrinsic",
],
[
"Std",
"Intrinsic",
],
],
[
[
"Std",
"Measurement",
],
[
"Std",
"Measurement",
],
],
[
[
"ns0",
Expand Down
8 changes: 4 additions & 4 deletions compiler/qsc_doc_gen/src/generate_docs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ fn docs_generation() {
let files = generate_docs(None, None, None);
let (_, metadata, contents) = files
.iter()
.find(|(file_name, _, _)| &**file_name == "Microsoft.Quantum.Core/Length.md")
.find(|(file_name, _, _)| &**file_name == "Std.Core/Length.md")
.expect("Could not file doc file for Length");
let full_contents = format!("{metadata}\n\n{contents}");

expect![[r#"
---
uid: Qdk.Microsoft.Quantum.Core.Length
uid: Qdk.Std.Core.Length
title: Length function
ms.date: {TIMESTAMP}
ms.topic: managed-reference
qsharp.kind: function
qsharp.namespace: Microsoft.Quantum.Core
qsharp.namespace: Std.Core
qsharp.name: Length
qsharp.summary: "Returns the number of elements in the input array `a`."
---

# Length function

Fully qualified name: Microsoft.Quantum.Core.Length
Fully qualified name: Std.Core.Length

```qsharp
function Length<'T>(a : 'T[]) : Int
Expand Down
Loading
Loading