Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions library/fixed_point/src/Main.qs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

export Init.PrepareFxP as PrepareFxP, Types.FixedPoint as FixedPoint;
export Init.PrepareFxP, Types.FixedPoint;

@Config(Unrestricted)
export Reciprocal.ComputeReciprocalFxP as ComputeReciprocalFxP;
export Reciprocal.ComputeReciprocalFxP;

@Config(FloatingPointComputations)
export Measurement.MeasureFxP as MeasureFxP;
export Measurement.MeasureFxP;
2 changes: 1 addition & 1 deletion library/rotations/src/Main.qs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

export HammingWeightPhasing.HammingWeightPhasing as HammingWeightPhasing;
export HammingWeightPhasing.HammingWeightPhasing;
2 changes: 1 addition & 1 deletion library/std/src/legacy_api.qs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Quantum {

namespace Microsoft.Quantum.Core {
import Std.Range.*;
export RangeStart, RangeEnd, IsRangeEmpty, Length, Repeated, Int, Qubit, Bool, Unit;
export RangeStart, RangeEnd, IsRangeEmpty, Length, Repeated;
}

namespace Microsoft.Quantum.Unstable {
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/qsc/src/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl Interpreter {
.expect("package should exist in the package store")
.package;
for global in global::iter_package(Some(map_fir_package_to_hir(package_id)), package) {
if let global::Kind::Term(term) = global.kind {
if let global::Kind::Callable(term) = global.kind {
let store_item_id = fir::StoreItemId {
package: package_id,
item: fir::LocalItemId::from(usize::from(term.id.item)),
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/qsc/src/interpret/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ mod given_interpreter {
&result,
&output,
&expect![[r#"
name error: `DumpMachine` could refer to the item in `Other` or `Microsoft.Quantum.Diagnostics`
name error: `DumpMachine` could refer to the item in `Other` or `Std.Diagnostics`
ambiguous name [line_3] [DumpMachine]
found in this namespace [line_1] [Other]
and also in this namespace [line_2] [Std.Diagnostics]
Expand Down
4 changes: 3 additions & 1 deletion source/compiler/qsc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub use qsc_formatter::formatter;
pub use qsc_frontend::compile::{CompileUnit, PackageStore, SourceContents, SourceMap, SourceName};

pub mod resolve {
pub use qsc_frontend::resolve::{Local, LocalKind, Locals, Res, path_as_field_accessor};
pub use qsc_frontend::resolve::{
Local, LocalKind, Locals, Res, iter_valid_items, path_as_field_accessor,
};
}

pub mod fir {
Expand Down
88 changes: 50 additions & 38 deletions source/compiler/qsc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ impl Display for ItemKind {
},
ItemKind::Ty(name, t) => write!(f, "New Type ({name}): {t}")?,
ItemKind::Struct(s) => write!(f, "{s}")?,
ItemKind::ImportOrExport(item) if item.is_export => write!(f, "Export ({item})")?,
ItemKind::ImportOrExport(item) => write!(f, "Import ({item})")?,
ItemKind::ImportOrExport(item) => write!(f, "{item}")?,
}
Ok(())
}
Expand Down Expand Up @@ -1889,13 +1888,18 @@ pub struct ImportOrExportDecl {

impl Display for ImportOrExportDecl {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let items_str = self
.items
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>()
.join(", ");
write!(f, "ImportOrExportDecl {}: [{items_str}]", self.span)
let mut indent = set_indentation(indented(f), 0);
if self.is_export {
write!(indent, "Export")?;
} else {
write!(indent, "Import")?;
}
write!(indent, " {}:", self.span)?;
indent = set_indentation(indent, 1);
for item in &self.items {
write!(indent, "\n{item}")?;
}
Ok(())
}
}

Expand All @@ -1921,39 +1925,32 @@ impl ImportOrExportDecl {
pub fn is_import(&self) -> bool {
!self.is_export
}

/// Returns an iterator over the items being exported from this namespace.
pub fn items(&self) -> impl Iterator<Item = &ImportOrExportItem> {
self.items.iter()
}
}

/// An individual item within an [`ImportOrExportDecl`]. This can be a path or a path with an alias.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct ImportOrExportItem {
/// The span of the import path including the glob and alias, if any.
/// The span of the import path including the wildcard and alias, if any.
pub span: Span,
/// The path to the item being exported.
pub path: PathKind,
/// An optional alias for the item being exported.
pub alias: Option<Ident>,
/// Whether this is a glob import/export.
pub is_glob: bool,
/// The kind of import being performed, direct or wildcard.
pub kind: ImportKind,
}

impl Display for ImportOrExportItem {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let ImportOrExportItem {
span: _,
path,
alias,
is_glob,
} = self;
let is_glob = if *is_glob { ".*" } else { "" };
match alias {
Some(alias) => write!(f, "{path}{is_glob} as {alias}",),
None => write!(f, "{path}{is_glob}"),
write!(f, "{} ", self.span)?;
match &self.kind {
ImportKind::Wildcard => write!(f, "Wildcard")?,
ImportKind::Direct { alias } => {
write!(f, "Direct")?;
if let Some(alias) = alias {
write!(f, " (alias: {alias})")?;
}
}
}
write!(f, ": {}", self.path)
}
}

Expand All @@ -1968,19 +1965,34 @@ impl ImportOrExportItem {
/// Returns `None` if the path has an error.
#[must_use]
pub fn name(&self) -> Option<&Ident> {
match &self.alias {
Some(_) => self.alias.as_ref(),
None => {
if let PathKind::Ok(path) = &self.path {
Some(&path.name)
} else {
None
}
}
match &self.kind {
ImportKind::Wildcard => None,
ImportKind::Direct { alias } => alias.as_ref().or_else(|| match &self.path {
PathKind::Ok(path) => Some(path.name.as_ref()),
PathKind::Err(_) => None,
}),
}
}
}

/// The kind of import being performed in an `ImportOrExportItem`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ImportKind {
/// A wildcard import: `import A.*`
Wildcard,
/// A direct import or export: `import A.B`, `export A`, etc.
Direct {
/// An optional alias for the item being imported.
alias: Option<Ident>,
},
}

impl Default for ImportKind {
fn default() -> Self {
ImportKind::Direct { alias: None }
}
}

/// A [`TypeParameter`] is a generic type variable with optional bounds (constraints).
#[derive(Default, Debug, PartialEq, Eq, Clone, Hash)]
pub struct TypeParameter {
Expand Down
33 changes: 21 additions & 12 deletions source/compiler/qsc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

use crate::ast::{
Attr, Block, CallableBody, CallableDecl, Expr, ExprKind, FieldAccess, FieldAssign, FieldDef,
FunctorExpr, FunctorExprKind, Ident, Item, ItemKind, Namespace, Package, Pat, PatKind, Path,
PathKind, QubitInit, QubitInitKind, SpecBody, SpecDecl, Stmt, StmtKind, StringComponent,
StructDecl, TopLevelNode, Ty, TyDef, TyDefKind, TyKind, TypeParameter,
FunctorExpr, FunctorExprKind, Ident, ImportKind, ImportOrExportItem, Item, ItemKind, Namespace,
Package, Pat, PatKind, Path, PathKind, QubitInit, QubitInitKind, SpecBody, SpecDecl, Stmt,
StmtKind, StringComponent, StructDecl, TopLevelNode, Ty, TyDef, TyDefKind, TyKind,
TypeParameter,
};
use qsc_data_structures::span::Span;

Expand Down Expand Up @@ -38,6 +39,10 @@ pub trait MutVisitor: Sized {
walk_struct_decl(self, decl);
}

fn visit_import_or_export(&mut self, item: &mut ImportOrExportItem) {
walk_import_or_export(self, item);
}

fn visit_field_def(&mut self, def: &mut FieldDef) {
walk_field_def(self, def);
}
Expand Down Expand Up @@ -128,15 +133,11 @@ pub fn walk_item(vis: &mut impl MutVisitor, item: &mut Item) {
vis.visit_ty_def(def);
}
ItemKind::Struct(decl) => vis.visit_struct_decl(decl),
ItemKind::ImportOrExport(export) => {
vis.visit_span(&mut export.span);
for item in &mut *export.items {
vis.visit_span(&mut item.span);
vis.visit_path_kind(&mut item.path);
if let Some(ref mut alias) = item.alias {
vis.visit_ident(alias);
}
}
ItemKind::ImportOrExport(decl) => {
vis.visit_span(&mut decl.span);
decl.items
.iter_mut()
.for_each(|item| vis.visit_import_or_export(item));
}
}
}
Expand Down Expand Up @@ -193,6 +194,14 @@ pub fn walk_struct_decl(vis: &mut impl MutVisitor, decl: &mut StructDecl) {
decl.fields.iter_mut().for_each(|f| vis.visit_field_def(f));
}

pub fn walk_import_or_export(vis: &mut impl MutVisitor, item: &mut ImportOrExportItem) {
vis.visit_span(&mut item.span);
vis.visit_path_kind(&mut item.path);
if let ImportKind::Direct { alias: Some(alias) } = &mut item.kind {
vis.visit_ident(alias);
}
}

pub fn walk_field_def(vis: &mut impl MutVisitor, def: &mut FieldDef) {
vis.visit_span(&mut def.span);
vis.visit_ident(&mut def.name);
Expand Down
31 changes: 21 additions & 10 deletions source/compiler/qsc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

use crate::ast::{
Attr, Block, CallableBody, CallableDecl, Expr, ExprKind, FieldAccess, FieldAssign, FieldDef,
FunctorExpr, FunctorExprKind, Ident, Item, ItemKind, Namespace, Package, Pat, PatKind, Path,
PathKind, QubitInit, QubitInitKind, SpecBody, SpecDecl, Stmt, StmtKind, StringComponent,
StructDecl, TopLevelNode, Ty, TyDef, TyDefKind, TyKind, TypeParameter,
FunctorExpr, FunctorExprKind, Ident, ImportKind, ImportOrExportItem, Item, ItemKind, Namespace,
Package, Pat, PatKind, Path, PathKind, QubitInit, QubitInitKind, SpecBody, SpecDecl, Stmt,
StmtKind, StringComponent, StructDecl, TopLevelNode, Ty, TyDef, TyDefKind, TyKind,
TypeParameter,
};

pub trait Visitor<'a>: Sized {
Expand Down Expand Up @@ -37,6 +38,10 @@ pub trait Visitor<'a>: Sized {
walk_struct_decl(self, decl);
}

fn visit_import_or_export(&mut self, item: &'a ImportOrExportItem) {
walk_import_or_export(self, item);
}

fn visit_field_def(&mut self, def: &'a FieldDef) {
walk_field_def(self, def);
}
Expand Down Expand Up @@ -120,16 +125,12 @@ pub fn walk_item<'a>(vis: &mut impl Visitor<'a>, item: &'a Item) {
}
ItemKind::Struct(decl) => vis.visit_struct_decl(decl),
ItemKind::ImportOrExport(decl) => {
for item in &decl.items {
vis.visit_path_kind(&item.path);
if let Some(ref alias) = item.alias {
vis.visit_ident(alias);
}
}
decl.items
.iter()
.for_each(|item| vis.visit_import_or_export(item));
}
}
}

pub fn walk_attr<'a>(vis: &mut impl Visitor<'a>, attr: &'a Attr) {
vis.visit_ident(&attr.name);
vis.visit_expr(&attr.arg);
Expand Down Expand Up @@ -174,6 +175,16 @@ pub fn walk_struct_decl<'a>(vis: &mut impl Visitor<'a>, decl: &'a StructDecl) {
decl.fields.iter().for_each(|f| vis.visit_field_def(f));
}

pub fn walk_import_or_export<'a>(vis: &mut impl Visitor<'a>, item: &'a ImportOrExportItem) {
vis.visit_path_kind(&item.path);
if let ImportKind::Direct {
alias: Some(ref alias),
} = item.kind
{
vis.visit_ident(alias);
}
}

pub fn walk_field_def<'a>(vis: &mut impl Visitor<'a>, def: &'a FieldDef) {
vis.visit_ident(&def.name);
vis.visit_ty(&def.ty);
Expand Down
15 changes: 7 additions & 8 deletions source/compiler/qsc_codegen/src/qsharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use std::vec;

use qsc_ast::ast::{
self, Attr, BinOp, Block, CallableBody, CallableDecl, CallableKind, Expr, ExprKind,
FieldAccess, Functor, FunctorExpr, FunctorExprKind, Ident, Idents, ImportOrExportItem, Item,
ItemKind, Lit, Mutability, Pat, PatKind, Path, PathKind, Pauli, QubitInit, QubitInitKind,
QubitSource, SetOp, SpecBody, SpecDecl, SpecGen, Stmt, StmtKind, StringComponent, TernOp,
TopLevelNode, Ty, TyDef, TyDefKind, TyKind, UnOp,
FieldAccess, Functor, FunctorExpr, FunctorExprKind, Ident, Idents, ImportKind,
ImportOrExportItem, Item, ItemKind, Lit, Mutability, Pat, PatKind, Path, PathKind, Pauli,
QubitInit, QubitInitKind, QubitSource, SetOp, SpecBody, SpecDecl, SpecGen, Stmt, StmtKind,
StringComponent, TernOp, TopLevelNode, Ty, TyDef, TyDefKind, TyKind, UnOp,
};
use qsc_ast::ast::{Namespace, Package};
use qsc_ast::visit::Visitor;
Expand Down Expand Up @@ -162,19 +162,18 @@ impl<W: Write> Visitor<'_> for QSharpGen<W> {
ImportOrExportItem {
span: _,
path,
is_glob,
alias,
kind,
},
) in decl.items.iter().enumerate()
{
let is_last = ix == decl.items.len() - 1;
self.visit_path_kind(path);

if *is_glob {
if let ImportKind::Wildcard = kind {
self.write(".*");
}

if let Some(alias) = alias {
if let ImportKind::Direct { alias: Some(alias) } = kind {
self.write(&format!(" as {}", alias.name));
}

Expand Down
4 changes: 2 additions & 2 deletions source/compiler/qsc_doc_gen/src/generate_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use qsc_data_structures::language_features::LanguageFeatures;
use qsc_data_structures::target::TargetCapabilityFlags;
use qsc_frontend::compile::{self, Dependencies, PackageStore, SourceMap, compile};
use qsc_frontend::resolve;
use qsc_hir::hir::{CallableKind, Item, ItemKind, Package, PackageId, Visibility};
use qsc_hir::hir::{CallableKind, Item, ItemKind, Package, PackageId, Res, Visibility};
use qsc_hir::{hir, ty};
use rustc_hash::FxHashMap;
use std::fmt::{Display, Formatter, Result};
Expand Down Expand Up @@ -650,7 +650,7 @@ fn resolve_export<'a>(
if matches!(item.kind, ItemKind::Namespace(_, _)) {
return None;
}
if let ItemKind::Export(_, id) = item.kind {
if let ItemKind::Export(_, Res::Item(id)) = item.kind {
let (exported_item, exported_package, _) =
display.compilation.resolve_item(default_package_id, &id);
return resolve_export(
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/qsc_fir/src/fir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ pub enum ItemKind {
/// A `newtype` declaration.
Ty(Ident, Udt),
/// An export referring to another item
Export(Ident, ItemId),
Export(Ident, Res),
}

impl Display for ItemKind {
Expand Down
Loading
Loading