Skip to content

Commit

Permalink
Measurement attribute on function issues error
Browse files Browse the repository at this point in the history
  • Loading branch information
orpuente-MS committed Oct 23, 2024
1 parent 408e8c3 commit eca101e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
43 changes: 30 additions & 13 deletions compiler/qsc_frontend/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ pub(super) enum Error {
#[error("invalid attribute arguments: expected {0}")]
#[diagnostic(code("Qsc.LowerAst.InvalidAttrArgs"))]
InvalidAttrArgs(String, #[label] Span),
#[error("invalid use of the Measurement attribute on a function")]
#[diagnostic(code("Qsc.LowerAst.InvalidMeasurementAttrOnFunction"))]
InvalidMeasurementAttrOnFunction(#[label] Span),
#[error("missing callable body")]
#[diagnostic(code("Qsc.LowerAst.MissingBody"))]
#[diagnostic(help("try declaring the callable as an operation"))]
MissingBody(#[label] Span),
#[error("duplicate specialization")]
#[diagnostic(code("Qsc.LowerAst.DuplicateSpec"))]
Expand Down Expand Up @@ -380,7 +384,7 @@ impl With<'_> {
attrs: &[qsc_hir::hir::Attr],
) -> hir::CallableDecl {
let id = self.lower_id(decl.id);
let kind = lower_callable_kind(decl.kind, attrs);
let kind = self.lower_callable_kind(decl.kind, attrs, decl.name.span);
let name = self.lower_ident(&decl.name);
let mut input = self.lower_pat(&decl.input);
let output = convert::ty_from_ast(self.names, &decl.output).0;
Expand Down Expand Up @@ -428,6 +432,30 @@ impl With<'_> {
}
}

fn lower_callable_kind(
&mut self,
kind: ast::CallableKind,
attrs: &[qsc_hir::hir::Attr],
span: Span,
) -> hir::CallableKind {
if attrs.contains(&qsc_hir::hir::Attr::Measurement) {
match kind {
ast::CallableKind::Operation => hir::CallableKind::Measurement,
ast::CallableKind::Function => {
self.lowerer
.errors
.push(Error::InvalidMeasurementAttrOnFunction(span));
hir::CallableKind::Function
}
}
} else {
match kind {
ast::CallableKind::Operation => hir::CallableKind::Operation,
ast::CallableKind::Function => hir::CallableKind::Function,
}
}
}

fn find_spec(
&mut self,
specs: &[Box<ast::SpecDecl>],
Expand Down Expand Up @@ -623,7 +651,7 @@ impl With<'_> {
FunctorSetValue::Empty
};
let lambda = Lambda {
kind: lower_callable_kind(*kind, &[]),
kind: self.lower_callable_kind(*kind, &[], expr.span),
functors,
input: self.lower_pat(input),
body: self.lower_expr(body),
Expand Down Expand Up @@ -934,17 +962,6 @@ impl With<'_> {
}
}

fn lower_callable_kind(kind: ast::CallableKind, attrs: &[qsc_hir::hir::Attr]) -> hir::CallableKind {
if attrs.contains(&qsc_hir::hir::Attr::Measurement) {
hir::CallableKind::Measurement
} else {
match kind {
ast::CallableKind::Function => hir::CallableKind::Function,
ast::CallableKind::Operation => hir::CallableKind::Operation,
}
}
}

fn lower_mutability(mutability: ast::Mutability) -> hir::Mutability {
match mutability {
ast::Mutability::Immutable => hir::Mutability::Immutable,
Expand Down
24 changes: 24 additions & 0 deletions compiler/qsc_frontend/src/lower/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,30 @@ fn invalid_spec_pat() {
);
}

#[test]
fn test_measurement_attr_on_function_issues_error() {
check_errors(
indoc! {r#"
namespace Test {
@Measurement()
function Foo(q: Qubit) : Result {
body intrinsic;
}
}
"#},
&expect![[r#"
[
InvalidMeasurementAttrOnFunction(
Span {
lo: 49,
hi: 52,
},
),
]
"#]],
);
}

#[test]
fn item_docs() {
check_hir(
Expand Down

0 comments on commit eca101e

Please sign in to comment.