Skip to content

Commit

Permalink
Remove old Num class constraint (#2031)
Browse files Browse the repository at this point in the history
  • Loading branch information
swernli authored Nov 16, 2024
1 parent f1a9e7a commit db9db4c
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 13 deletions.
4 changes: 3 additions & 1 deletion compiler/qsc_frontend/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ pub(super) enum Error {
span: Span,
},
#[error("unrecognized class constraint {name}")]
#[help("supported classes are Eq, Add, Exp, Integral, Num, and Show")]
#[help(
"supported classes are Eq, Add, Sub, Mul, Div, Mod, Signed, Ord, Exp, Integral, and Show"
)]
#[diagnostic(code("Qsc.LowerAst.UnrecognizedClass"))]
UnrecognizedClass {
#[label]
Expand Down
4 changes: 3 additions & 1 deletion compiler/qsc_frontend/src/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ enum ErrorKind {
span: Span,
},
#[error("unrecognized class constraint {name}")]
#[help("supported classes are Eq, Add, Exp, Integral, Num, and Show")]
#[help(
"supported classes are Eq, Add, Sub, Mul, Div, Mod, Signed, Ord, Exp, Integral, and Show"
)]
#[diagnostic(code("Qsc.TypeCk.UnrecognizedClass"))]
UnrecognizedClass {
#[label]
Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_frontend/src/typeck/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ fn check_param_length(
) -> bool {
let num_given_parameters = bound.parameters.len();
let num_parameters = match &*bound.name.name {
"Eq" | "Add" | "Integral" | "Num" | "Show" => 0,
"Eq" | "Add" | "Integral" | "Mod" | "Mul" | "Sub" | "Div" | "Signed" | "Ord" | "Show" => 0,
"Iterable" | "Exp" => 1,
_ => return false,
};
Expand Down
1 change: 0 additions & 1 deletion language_service/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ fn collect_names(
CompletionItemKind::Class,
Some("Exp['Power]".into()),
),
Completion::new("Num".to_string(), CompletionItemKind::Class),
Completion::new("Integral".to_string(), CompletionItemKind::Class),
Completion::new("Show".to_string(), CompletionItemKind::Class),
Completion::new("Signed".to_string(), CompletionItemKind::Class),
Expand Down
147 changes: 141 additions & 6 deletions language_service/src/completion/tests/class_completions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#![allow(clippy::too_many_lines)]

use super::check;
use expect_test::expect;

Expand All @@ -17,13 +22,31 @@ fn iterable_not_included_in_completions() {
);
}

// the `Num` class should not be in completions since it was dropped
#[test]
fn num_not_included_in_completions() {
check(
r"namespace Test {
operation Test<'T: ↘
}",
&["Num"],
&expect![[r#"
[
None,
]
"#]],
);
}

#[test]
fn all_prim_classes_in_completions() {
check(
r"namespace Test {
operation Test<'T: ↘
}",
&["Eq", "Add", "Exp", "Integral", "Num", "Show"],
&[
"Eq", "Add", "Exp", "Integral", "Mod", "Mul", "Sub", "Div", "Signed", "Ord", "Show",
],
&expect![[r#"
[
Some(
Expand Down Expand Up @@ -74,10 +97,65 @@ fn all_prim_classes_in_completions() {
),
Some(
CompletionItem {
label: "Num",
label: "Mod",
kind: Class,
sort_text: Some(
"0100Num",
"0100Mod",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Mul",
kind: Class,
sort_text: Some(
"0100Mul",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Sub",
kind: Class,
sort_text: Some(
"0100Sub",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Div",
kind: Class,
sort_text: Some(
"0100Div",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Signed",
kind: Class,
sort_text: Some(
"0100Signed",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Ord",
kind: Class,
sort_text: Some(
"0100Ord",
),
detail: None,
additional_text_edits: None,
Expand Down Expand Up @@ -105,7 +183,9 @@ fn classes_appear_after_plus_too() {
r"namespace Test {
operation Test<'T: Add + ↘
}",
&["Eq", "Add", "Exp", "Integral", "Num", "Show"],
&[
"Eq", "Add", "Exp", "Integral", "Mod", "Mul", "Sub", "Div", "Signed", "Ord", "Show",
],
&expect![[r#"
[
Some(
Expand Down Expand Up @@ -156,10 +236,65 @@ fn classes_appear_after_plus_too() {
),
Some(
CompletionItem {
label: "Num",
label: "Mod",
kind: Class,
sort_text: Some(
"0100Mod",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Mul",
kind: Class,
sort_text: Some(
"0100Mul",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Sub",
kind: Class,
sort_text: Some(
"0100Sub",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Div",
kind: Class,
sort_text: Some(
"0100Div",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Signed",
kind: Class,
sort_text: Some(
"0100Signed",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Ord",
kind: Class,
sort_text: Some(
"0100Num",
"0100Ord",
),
detail: None,
additional_text_edits: None,
Expand Down
4 changes: 1 addition & 3 deletions samples/language/ClassConstraints.qs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// # Description
// Q# supports constraining generic types via _class constraints_. The formal term for this concept is bounded polymorphism,
// or parametric polymorphism.
// The currently supported classes are `Exp`, for exponentiation; `Eq`, for comparison via the `==` operator; `Add`, for addition via the `+` operator;
// `Num`, if a type is numeric; `Integral`, if a type is a form of integer; and `Show`, if a type can be rendered as a string.

// A generic type, or type parameter, is specified on a callable declaration to signify that a function can take multiple types of data as input.
// For a generic type parameter to be useful, we need to be able to know enough about it to operate on it. This is where class constraints come in. By specifying
Expand All @@ -14,12 +12,12 @@
// Classes that Q# currently supports are:
// - `Eq`: denotes that a type can be compared to other values of the same type via the `==` operator.
// - `Add`: denotes that a type can be added to other values of the same type via the `+` operator.
// - `Mul`: denotes that a type can be used with the multiplication (`*`) operator.
// - `Mod`: denotes that a type can be used with the modulo (`%`) operator.
// - `Sub`: denotes that a type can subtracted from other values of the same type via the `-` operator.
// - `Div`: denotes that a type can subtracted from other values of the same type via the `/` operator.
// - `Signed`: denotes that a type can be negated or made positive with `+` or `-` unary operator prefixes.
// - `Ord`: denotes that a type can be used with comparison operators (`>`, `<`, `>=`, `<=`).
// - `Num`: denotes that a type can be used in `>`, `>=`, `<`, `<=`, `/`, `%`, `*`, and `-` operator expressions.
// - `Show`: denotes that a type can be converted to a string via format strings (`$"number: {num}"`).
// - `Exp['T]`: denotes that a type can be raised to a power of type `'T`. The return type of exponentiation is the type of the base.
// - `Integral`: denotes that a type is an integer-ish type, i.e., can be used in following expressions using the following operators: `&&&`, `|||`, `^^^`, `<<<`, and `>>>`.
Expand Down

0 comments on commit db9db4c

Please sign in to comment.