tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

139 lignes · 2828 octetsBrut · Historique
1pub type Light { Red, Amber, Green }
2
3pub fn complete :: l: Light -> Int
4{
5 match l { Red -> 0, Amber -> 1, Green -> 2 }
6}
7
8pub fn missing_variant :: l: Light -> Int
9{
10 match l //~ ERROR E0301 `Green` is not covered
11 { Red -> 0, Amber -> 1 }
12}
13
14pub fn nested :: o: Option<Option<Int>> -> Int
15{
16 match o //~ ERROR E0301 `Some(None)` is not covered
17 { None -> 0, Some(Some(n)) -> n }
18}
19
20pub fn lists :: xs: List<Int> -> Int
21{
22 match xs //~ ERROR E0301 `[_, ..]` is not covered
23 { [] -> 0, [x] -> x, [_, _, ..] if True -> 2 }
24}
25
26pub fn tuples :: p: (Bool, Bool) -> Int
27{
28 match p //~ ERROR E0301 `(False, False)` is not covered
29 { (True, _) -> 1, (_, True) -> 2 }
30}
31
32pub fn literals :: n: Int -> String
33{
34 match n //~ ERROR E0301 `_` is not covered
35 { 0 -> "zero", 1 -> "one" }
36}
37
38pub fn literals_ok :: n: Int -> String
39{
40 match n { 0 -> "zero", _ -> "other" }
41}
42
43pub fn or_patterns :: l: Light -> Bool
44{
45 match l { Red | Amber -> False, Green -> True }
46}
47
48pub fn unreachable :: l: Light -> Int
49{
50 match l
51 { _ -> 0
52 , Red -> 1 //~ WARN W0301 unreachable
53 }
54}
55
56pub fn unreachable_after_cover :: o: Option<Int> -> Int
57{
58 match o
59 { Some(_) -> 1
60 , None -> 0
61 , Some(5) -> 5 //~ WARN W0301 unreachable
62 }
63}
64
65pub fn labelled :: u: Option<Light> -> Int
66{
67 match u { Some(Red) -> 0, Some(_) -> 1, None -> 2 }
68}
69
70pub fn refutable_let :: o: Option<Int> -> Int
71{
72 let Some(x) = o //~ ERROR E0302 `None` is not covered
73 x
74}
75
76pub fn irrefutable_let :: p: (Int, Int) -> Int
77{
78 let (a, b) = p
79 a + b
80}
81
82pub fn never_needs_no_arms :: n: Never -> Int
83{
84 match n {}
85}
86
87// A bodyless type has no constructors either (ADR 0059); `@builtin`
88// types like `Int` stay inhabited (see `int_needs_arms`).
89type Foreign
90
91pub fn bodyless_needs_no_arms :: x: Foreign -> Int
92{
93 match x {}
94}
95
96// Without arms, the scrutinee's type decides (issue #218).
97pub fn int_needs_arms :: n: Int -> String
98{
99 match n {} //~ ERROR E0301 `_` is not covered
100}
101
102pub fn bool_needs_arms :: b: Bool -> String
103{
104 match b {} //~ ERROR E0301 `False` is not covered
105}
106
107pub fn any_type_needs_arms :: x: a -> Int
108{
109 match x {} //~ ERROR E0301 `_` is not covered
110}
111
112pub fn result_missing_error :: r: Result<Int, String> -> Int
113{
114 match r //~ ERROR E0301 `Error(_)` is not covered
115 { Ok(n) -> n }
116}
117
118pub fn result_missing_ok :: r: Result<Int, String> -> Int
119{
120 match r //~ ERROR E0301 `Ok(_)` is not covered
121 { Error(_) -> 0 }
122}
123
124pub fn result_nested :: r: Result<Option<Int>, String> -> Int
125{
126 match r //~ ERROR E0301 `Ok(None)` is not covered
127 { Ok(Some(n)) -> n, Error(_) -> 0 }
128}
129
130pub fn result_complete :: r: Result<Result<Int, String>, String> -> Int
131{
132 match r { Ok(Ok(n)) -> n, Ok(Error(_)) | Error(_) -> 0 }
133}
134
135pub fn result_refutable_let :: r: Result<Int, String> -> Int
136{
137 let Ok(n) = r //~ ERROR E0302 `Error(_)` is not covered
138 n
139}