Skip to content

Commit fff1a27

Browse files
committed
Ty names are now human readable for errors
1 parent c18bbc1 commit fff1a27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2563
-2669
lines changed

compiler/qsc_qasm/src/semantic/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ pub enum SemanticErrorKind {
166166
#[error("{0} were introduced in version {1}")]
167167
#[diagnostic(code("Qasm.Lowerer.NotSupportedInThisVersion"))]
168168
NotSupportedInThisVersion(String, String, #[label] Span),
169-
#[error("the operator {0} is not valid with lhs {1} and rhs {2}")]
170-
#[diagnostic(code("Qasm.Lowerer.OperatorNotSupportedForTypes"))]
171-
OperatorNotSupportedForTypes(String, String, String, #[label] Span),
169+
#[error("the operator {0} is not allowed for complex values")]
170+
#[diagnostic(code("Qasm.Lowerer.OperatorNotAllowedForComplexValues"))]
171+
OperatorNotAllowedForComplexValues(String, #[label] Span),
172172
#[error("pow gate modifiers must have an exponent")]
173173
#[diagnostic(code("Qasm.Lowerer.PowModifierMustHaveExponent"))]
174174
PowModifierMustHaveExponent(#[label] Span),

compiler/qsc_qasm/src/semantic/lowerer.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ impl Lowerer {
10341034
Type::Void => crate::types::Type::Tuple(vec![]),
10351035
Type::Err => crate::types::Type::Err,
10361036
_ => {
1037-
let msg = format!("converting {ty:?} to Q# type");
1037+
let msg = format!("converting `{ty}` to Q# type");
10381038
self.push_unimplemented_error_message(msg, span);
10391039
crate::types::Type::Err
10401040
}
@@ -2753,7 +2753,7 @@ impl Lowerer {
27532753
)))
27542754
}
27552755
Type::Gate(_, _) | Type::Function(..) | Type::Range | Type::Set | Type::Void => {
2756-
let message = format!("default values for {ty:?}");
2756+
let message = format!("default values for {ty}");
27572757
self.push_unsupported_error_message(message, span);
27582758
None
27592759
}
@@ -3280,21 +3280,15 @@ impl Lowerer {
32803280
let Some(ty) = ty else {
32813281
let target_ty = Type::UInt(None, left_type.is_const() && right_type.is_const());
32823282
if lhs_uint_promotion.is_none() {
3283-
let target_str: String = format!("{target_ty:?}");
3284-
let kind = SemanticErrorKind::CannotCast(
3285-
format!("{left_type:?}"),
3286-
target_str,
3287-
lhs.span,
3288-
);
3283+
let target_str: String = target_ty.to_string();
3284+
let kind =
3285+
SemanticErrorKind::CannotCast(left_type.to_string(), target_str, lhs.span);
32893286
self.push_semantic_error(kind);
32903287
}
32913288
if rhs_uint_promotion.is_none() {
3292-
let target_str: String = format!("{target_ty:?}");
3293-
let kind = SemanticErrorKind::CannotCast(
3294-
format!("{right_type:?}"),
3295-
target_str,
3296-
rhs.span,
3297-
);
3289+
let target_str = target_ty.to_string();
3290+
let kind =
3291+
SemanticErrorKind::CannotCast(right_type.to_string(), target_str, rhs.span);
32983292
self.push_semantic_error(kind);
32993293
}
33003294
let bin_expr = semantic::BinaryOpExpr {
@@ -3440,12 +3434,8 @@ impl Lowerer {
34403434
ty: ty.clone(),
34413435
}
34423436
} else {
3443-
let kind = SemanticErrorKind::OperatorNotSupportedForTypes(
3444-
format!("{op:?}"),
3445-
format!("{ty:?}"),
3446-
format!("{ty:?}"),
3447-
span,
3448-
);
3437+
let kind =
3438+
SemanticErrorKind::OperatorNotAllowedForComplexValues(format!("{op:?}"), span);
34493439
self.push_semantic_error(kind);
34503440
err_expr!(ty.clone())
34513441
}
@@ -3680,7 +3670,7 @@ impl Lowerer {
36803670
indices: &[semantic::Index],
36813671
) -> super::types::Type {
36823672
if !ty.is_array() {
3683-
let kind = SemanticErrorKind::CannotIndexType(format!("{ty:?}"), span);
3673+
let kind = SemanticErrorKind::CannotIndexType(ty.to_string(), span);
36843674
self.push_semantic_error(kind);
36853675
return super::types::Type::Err;
36863676
}
@@ -3696,7 +3686,7 @@ impl Lowerer {
36963686
} else {
36973687
// we failed to get the indexed type, unknown error
36983688
// we should have caught this earlier with the two checks above
3699-
let kind = SemanticErrorKind::CannotIndexType(format!("{ty:?}"), span);
3689+
let kind = SemanticErrorKind::CannotIndexType(ty.to_string(), span);
37003690
self.push_semantic_error(kind);
37013691
super::types::Type::Err
37023692
}
@@ -3796,15 +3786,15 @@ impl Lowerer {
37963786
}
37973787

37983788
fn push_invalid_cast_error(&mut self, target_ty: &Type, expr_ty: &Type, span: Span) {
3799-
let rhs_ty_name = format!("{expr_ty:?}");
3800-
let lhs_ty_name = format!("{target_ty:?}");
3789+
let rhs_ty_name = expr_ty.to_string();
3790+
let lhs_ty_name = target_ty.to_string();
38013791
let kind = SemanticErrorKind::CannotCast(rhs_ty_name, lhs_ty_name, span);
38023792
self.push_semantic_error(kind);
38033793
}
38043794

38053795
fn push_invalid_literal_cast_error(&mut self, target_ty: &Type, expr_ty: &Type, span: Span) {
3806-
let rhs_ty_name = format!("{expr_ty:?}");
3807-
let lhs_ty_name = format!("{target_ty:?}");
3796+
let rhs_ty_name = expr_ty.to_string();
3797+
let lhs_ty_name = target_ty.to_string();
38083798
let kind = SemanticErrorKind::CannotCastLiteral(rhs_ty_name, lhs_ty_name, span);
38093799
self.push_semantic_error(kind);
38103800
}

compiler/qsc_qasm/src/semantic/tests.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,61 +193,61 @@ fn semantic_errors_map_to_their_corresponding_file_specific_spans() {
193193
symbol_id: 32
194194
ty_span: [196-199]
195195
init_expr: Expr [204-205]:
196-
ty: Bit(true)
196+
ty: const bit
197197
kind: Lit: Bit(1)
198198
Stmt [211-227]:
199199
annotations: <empty>
200200
kind: ClassicalDeclarationStmt [211-227]:
201201
symbol_id: 32
202202
ty_span: [211-215]
203203
init_expr: Expr [220-226]:
204-
ty: Bool(false)
204+
ty: bool
205205
kind: BinaryOpExpr:
206206
op: AndL
207207
lhs: Expr [220-221]:
208-
ty: Err
208+
ty: unknown
209209
kind: SymbolId(33)
210210
rhs: Expr [225-226]:
211-
ty: Bool(false)
211+
ty: bool
212212
kind: Cast [0-0]:
213-
ty: Bool(false)
213+
ty: bool
214214
expr: Expr [225-226]:
215-
ty: Bit(false)
215+
ty: bit
216216
kind: SymbolId(32)
217217
Stmt [140-154]:
218218
annotations: <empty>
219219
kind: ClassicalDeclarationStmt [140-154]:
220220
symbol_id: 34
221221
ty_span: [140-145]
222222
init_expr: Expr [150-153]:
223-
ty: Angle(None, true)
223+
ty: const angle
224224
kind: Lit: Angle(0.7168146928204138)
225225
Stmt [159-179]:
226226
annotations: <empty>
227227
kind: ClassicalDeclarationStmt [159-179]:
228228
symbol_id: 35
229229
ty_span: [159-164]
230230
init_expr: Expr [169-178]:
231-
ty: Float(None, false)
231+
ty: float
232232
kind: BinaryOpExpr:
233233
op: Add
234234
lhs: Expr [169-170]:
235-
ty: Angle(None, false)
235+
ty: angle
236236
kind: SymbolId(34)
237237
rhs: Expr [173-178]:
238-
ty: Float(None, false)
238+
ty: float
239239
kind: Cast [0-0]:
240-
ty: Float(None, false)
240+
ty: float
241241
expr: Expr [173-178]:
242-
ty: Bool(true)
242+
ty: const bool
243243
kind: Lit: Bool(false)
244244
Stmt [74-84]:
245245
annotations: <empty>
246246
kind: ClassicalDeclarationStmt [74-84]:
247247
symbol_id: 37
248248
ty_span: [74-77]
249249
init_expr: Expr [82-83]:
250-
ty: Err
250+
ty: unknown
251251
kind: SymbolId(36)
252252
253253
[Qasm.Lowerer.UndefinedSymbol
@@ -260,7 +260,7 @@ fn semantic_errors_map_to_their_corresponding_file_specific_spans() {
260260
`----
261261
, Qasm.Lowerer.CannotCast
262262
263-
x cannot cast expression of type Err to type Bool(false)
263+
x cannot cast expression of type unknown to type bool
264264
,-[source2.qasm:2:14]
265265
1 | bit l = 1;
266266
2 | bool l = v && l; // undefined y, redefine l
@@ -276,8 +276,7 @@ fn semantic_errors_map_to_their_corresponding_file_specific_spans() {
276276
`----
277277
, Qasm.Lowerer.CannotCast
278278
279-
x cannot cast expression of type Angle(None, false) to type Float(None,
280-
| false)
279+
x cannot cast expression of type angle to type float
281280
,-[source1.qasm:3:15]
282281
2 | angle j = 7.0;
283282
3 | float k = j + false; // invalid cast
@@ -294,7 +293,7 @@ fn semantic_errors_map_to_their_corresponding_file_specific_spans() {
294293
`----
295294
, Qasm.Lowerer.CannotCast
296295
297-
x cannot cast expression of type Err to type Bit(false)
296+
x cannot cast expression of type unknown to type bit
298297
,-[source0.qasm:4:13]
299298
3 | include "source1.qasm";
300299
4 | bit c = r; // undefined symbol r

compiler/qsc_qasm/src/semantic/tests/assignment.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,52 @@ fn too_many_indices_in_indexed_assignment() {
2929
ty: FloatArray(Some(32), One(2))
3030
kind: Lit: array:
3131
Expr [45-48]:
32-
ty: Float(Some(32), false)
32+
ty: float[32]
3333
kind: Cast [0-0]:
34-
ty: Float(Some(32), false)
34+
ty: float[32]
3535
expr: Expr [45-48]:
36-
ty: Float(None, true)
36+
ty: const float
3737
kind: Lit: Float(1.1)
3838
Expr [50-53]:
39-
ty: Float(Some(32), false)
39+
ty: float[32]
4040
kind: Cast [0-0]:
41-
ty: Float(Some(32), false)
41+
ty: float[32]
4242
expr: Expr [50-53]:
43-
ty: Float(None, true)
43+
ty: const float
4444
kind: Lit: Float(1.2)
4545
Expr [56-66]:
4646
ty: FloatArray(Some(32), One(2))
4747
kind: Lit: array:
4848
Expr [57-60]:
49-
ty: Float(Some(32), false)
49+
ty: float[32]
5050
kind: Cast [0-0]:
51-
ty: Float(Some(32), false)
51+
ty: float[32]
5252
expr: Expr [57-60]:
53-
ty: Float(None, true)
53+
ty: const float
5454
kind: Lit: Float(2.1)
5555
Expr [62-65]:
56-
ty: Float(Some(32), false)
56+
ty: float[32]
5757
kind: Cast [0-0]:
58-
ty: Float(Some(32), false)
58+
ty: float[32]
5959
expr: Expr [62-65]:
60-
ty: Float(None, true)
60+
ty: const float
6161
kind: Lit: Float(2.2)
6262
Expr [68-78]:
6363
ty: FloatArray(Some(32), One(2))
6464
kind: Lit: array:
6565
Expr [69-72]:
66-
ty: Float(Some(32), false)
66+
ty: float[32]
6767
kind: Cast [0-0]:
68-
ty: Float(Some(32), false)
68+
ty: float[32]
6969
expr: Expr [69-72]:
70-
ty: Float(None, true)
70+
ty: const float
7171
kind: Lit: Float(3.1)
7272
Expr [74-77]:
73-
ty: Float(Some(32), false)
73+
ty: float[32]
7474
kind: Cast [0-0]:
75-
ty: Float(Some(32), false)
75+
ty: float[32]
7676
expr: Expr [74-77]:
77-
ty: Float(None, true)
77+
ty: const float
7878
kind: Lit: Float(3.2)
7979
Stmt [89-113]:
8080
annotations: <empty>
@@ -85,16 +85,16 @@ fn too_many_indices_in_indexed_assignment() {
8585
index_span: [97-106]
8686
indices:
8787
Expr [98-99]:
88-
ty: Int(None, true)
88+
ty: const int
8989
kind: Lit: Int(1)
9090
Expr [101-102]:
91-
ty: Int(None, true)
91+
ty: const int
9292
kind: Lit: Int(1)
9393
Expr [104-105]:
94-
ty: Int(None, true)
94+
ty: const int
9595
kind: Lit: Int(3)
9696
rhs: Expr [109-112]:
97-
ty: Float(None, true)
97+
ty: const float
9898
kind: Lit: Float(2.3)
9999
100100
[Qasm.Lowerer.TooManyIndices
@@ -108,7 +108,7 @@ fn too_many_indices_in_indexed_assignment() {
108108
`----
109109
, Qasm.Lowerer.CannotCastLiteral
110110
111-
x cannot cast literal expression of type Float(None, true) to type Err
111+
x cannot cast literal expression of type const float to type unknown
112112
,-[test:3:9]
113113
2 | array[float[32], 3, 2] multiDim = {{1.1, 1.2}, {2.1, 2.2}, {3.1, 3.2}};
114114
3 | multiDim[1, 1, 3] = 2.3;

compiler/qsc_qasm/src/semantic/tests/decls.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn scalar_ty_designator_must_be_positive() {
6666
symbol_id: 8
6767
ty_span: [0-7]
6868
init_expr: Expr [0-0]:
69-
ty: Err
69+
ty: unknown
7070
kind: Err
7171
7272
[Qasm.Lowerer.TypeWidthMustBePositiveIntConstExpr
@@ -94,20 +94,20 @@ fn scalar_ty_designator_must_be_castable_to_const_int() {
9494
symbol_id: 8
9595
ty_span: [6-11]
9696
init_expr: Expr [19-22]:
97-
ty: Angle(None, true)
97+
ty: const angle
9898
kind: Lit: Angle(2.0000000000000004)
9999
Stmt [24-36]:
100100
annotations: <empty>
101101
kind: ClassicalDeclarationStmt [24-36]:
102102
symbol_id: 9
103103
ty_span: [24-33]
104104
init_expr: Expr [0-0]:
105-
ty: Err
105+
ty: unknown
106106
kind: Err
107107
108108
[Qasm.Lowerer.CannotCast
109109
110-
x cannot cast expression of type Angle(None, true) to type UInt(None, true)
110+
x cannot cast expression of type const angle to type const uint
111111
,-[test:1:29]
112112
1 | const angle size = 2.0; int[size] i;
113113
: ^^^^

0 commit comments

Comments
 (0)