Skip to content

Commit 954e2c0

Browse files
committed
allow const fn to be c-variadic
however `Drop` for `VaList` is not yet available in const fn
1 parent b2dcf7b commit 954e2c0

5 files changed

Lines changed: 23 additions & 77 deletions

File tree

compiler/rustc_ast_passes/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ ast_passes_c_variadic_no_extern = `...` is not supported for non-extern function
8282
8383
ast_passes_c_variadic_not_supported = the `{$target}` target does not support c-variadic functions
8484
85-
ast_passes_const_and_c_variadic = functions cannot be both `const` and C-variadic
86-
.const = `const` because of this
87-
.variadic = C-variadic because of this
88-
8985
ast_passes_const_and_coroutine = functions cannot be both `const` and `{$coroutine_kind}`
9086
.const = `const` because of this
9187
.coroutine = `{$coroutine_kind}` because of this

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -697,15 +697,6 @@ impl<'a> AstValidator<'a> {
697697
unreachable!("C variable argument list cannot be used in closures")
698698
};
699699

700-
// C-variadics are not yet implemented in const evaluation.
701-
if let Const::Yes(const_span) = sig.header.constness {
702-
self.dcx().emit_err(errors::ConstAndCVariadic {
703-
spans: vec![const_span, variadic_param.span],
704-
const_span,
705-
variadic_span: variadic_param.span,
706-
});
707-
}
708-
709700
if let Some(coroutine_kind) = sig.header.coroutine_kind {
710701
self.dcx().emit_err(errors::CoroutineAndCVariadic {
711702
spans: vec![coroutine_kind.span(), variadic_param.span],

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -710,17 +710,6 @@ pub(crate) struct ConstAndCoroutine {
710710
pub coroutine_kind: &'static str,
711711
}
712712

713-
#[derive(Diagnostic)]
714-
#[diag(ast_passes_const_and_c_variadic)]
715-
pub(crate) struct ConstAndCVariadic {
716-
#[primary_span]
717-
pub spans: Vec<Span>,
718-
#[label(ast_passes_const)]
719-
pub const_span: Span,
720-
#[label(ast_passes_variadic)]
721-
pub variadic_span: Span,
722-
}
723-
724713
#[derive(Diagnostic)]
725714
#[diag(ast_passes_coroutine_and_c_variadic)]
726715
pub(crate) struct CoroutineAndCVariadic {

tests/ui/parser/variadic-ffi-semantic-restrictions.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,14 @@ extern "C" fn f3_3(_: ..., x: isize) {}
3131
//~^ ERROR `...` must be the last argument of a C-variadic function
3232

3333
const unsafe extern "C" fn f4_1(x: isize, _: ...) {}
34-
//~^ ERROR functions cannot be both `const` and C-variadic
35-
//~| ERROR destructor of `VaList<'_>` cannot be evaluated at compile-time
34+
//~^ ERROR destructor of `VaList<'_>` cannot be evaluated at compile-time
3635

3736
const extern "C" fn f4_2(x: isize, _: ...) {}
38-
//~^ ERROR functions cannot be both `const` and C-variadic
39-
//~| ERROR functions with a C variable argument list must be unsafe
37+
//~^ ERROR functions with a C variable argument list must be unsafe
4038
//~| ERROR destructor of `VaList<'_>` cannot be evaluated at compile-time
4139

4240
const extern "C" fn f4_3(_: ..., x: isize, _: ...) {}
43-
//~^ ERROR functions cannot be both `const` and C-variadic
44-
//~| ERROR functions with a C variable argument list must be unsafe
41+
//~^ ERROR functions with a C variable argument list must be unsafe
4542
//~| ERROR `...` must be the last argument of a C-variadic function
4643

4744
extern "C" {
@@ -64,7 +61,6 @@ impl X {
6461
//~| ERROR `...` must be the last argument of a C-variadic function
6562
const fn i_f5(x: isize, _: ...) {}
6663
//~^ ERROR `...` is not supported for non-extern functions
67-
//~| ERROR functions cannot be both `const` and C-variadic
6864
//~| ERROR destructor of `VaList<'_>` cannot be evaluated at compile-time
6965
}
7066

tests/ui/parser/variadic-ffi-semantic-restrictions.stderr

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,8 @@ error: `...` must be the last argument of a C-variadic function
8080
LL | extern "C" fn f3_3(_: ..., x: isize) {}
8181
| ^^^^^^
8282

83-
error: functions cannot be both `const` and C-variadic
84-
--> $DIR/variadic-ffi-semantic-restrictions.rs:33:1
85-
|
86-
LL | const unsafe extern "C" fn f4_1(x: isize, _: ...) {}
87-
| ^^^^^ `const` because of this ^^^^^^ C-variadic because of this
88-
89-
error: functions cannot be both `const` and C-variadic
90-
--> $DIR/variadic-ffi-semantic-restrictions.rs:37:1
91-
|
92-
LL | const extern "C" fn f4_2(x: isize, _: ...) {}
93-
| ^^^^^ `const` because of this ^^^^^^ C-variadic because of this
94-
9583
error: functions with a C variable argument list must be unsafe
96-
--> $DIR/variadic-ffi-semantic-restrictions.rs:37:36
84+
--> $DIR/variadic-ffi-semantic-restrictions.rs:36:36
9785
|
9886
LL | const extern "C" fn f4_2(x: isize, _: ...) {}
9987
| ^^^^^^
@@ -104,19 +92,13 @@ LL | const unsafe extern "C" fn f4_2(x: isize, _: ...) {}
10492
| ++++++
10593

10694
error: `...` must be the last argument of a C-variadic function
107-
--> $DIR/variadic-ffi-semantic-restrictions.rs:42:26
95+
--> $DIR/variadic-ffi-semantic-restrictions.rs:40:26
10896
|
10997
LL | const extern "C" fn f4_3(_: ..., x: isize, _: ...) {}
11098
| ^^^^^^
11199

112-
error: functions cannot be both `const` and C-variadic
113-
--> $DIR/variadic-ffi-semantic-restrictions.rs:42:1
114-
|
115-
LL | const extern "C" fn f4_3(_: ..., x: isize, _: ...) {}
116-
| ^^^^^ `const` because of this ^^^^^^ C-variadic because of this
117-
118100
error: functions with a C variable argument list must be unsafe
119-
--> $DIR/variadic-ffi-semantic-restrictions.rs:42:44
101+
--> $DIR/variadic-ffi-semantic-restrictions.rs:40:44
120102
|
121103
LL | const extern "C" fn f4_3(_: ..., x: isize, _: ...) {}
122104
| ^^^^^^
@@ -127,111 +109,103 @@ LL | const unsafe extern "C" fn f4_3(_: ..., x: isize, _: ...) {}
127109
| ++++++
128110

129111
error: `...` must be the last argument of a C-variadic function
130-
--> $DIR/variadic-ffi-semantic-restrictions.rs:48:13
112+
--> $DIR/variadic-ffi-semantic-restrictions.rs:45:13
131113
|
132114
LL | fn e_f2(..., x: isize);
133115
| ^^^
134116

135117
error: `...` is not supported for non-extern functions
136-
--> $DIR/variadic-ffi-semantic-restrictions.rs:55:23
118+
--> $DIR/variadic-ffi-semantic-restrictions.rs:52:23
137119
|
138120
LL | fn i_f1(x: isize, _: ...) {}
139121
| ^^^^^^
140122
|
141123
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
142124

143125
error: `...` is not supported for non-extern functions
144-
--> $DIR/variadic-ffi-semantic-restrictions.rs:57:13
126+
--> $DIR/variadic-ffi-semantic-restrictions.rs:54:13
145127
|
146128
LL | fn i_f2(_: ...) {}
147129
| ^^^^^^
148130
|
149131
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
150132

151133
error: `...` must be the last argument of a C-variadic function
152-
--> $DIR/variadic-ffi-semantic-restrictions.rs:59:13
134+
--> $DIR/variadic-ffi-semantic-restrictions.rs:56:13
153135
|
154136
LL | fn i_f3(_: ..., x: isize, _: ...) {}
155137
| ^^^^^^
156138

157139
error: `...` is not supported for non-extern functions
158-
--> $DIR/variadic-ffi-semantic-restrictions.rs:59:31
140+
--> $DIR/variadic-ffi-semantic-restrictions.rs:56:31
159141
|
160142
LL | fn i_f3(_: ..., x: isize, _: ...) {}
161143
| ^^^^^^
162144
|
163145
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
164146

165147
error: `...` must be the last argument of a C-variadic function
166-
--> $DIR/variadic-ffi-semantic-restrictions.rs:62:13
148+
--> $DIR/variadic-ffi-semantic-restrictions.rs:59:13
167149
|
168150
LL | fn i_f4(_: ..., x: isize, _: ...) {}
169151
| ^^^^^^
170152

171153
error: `...` is not supported for non-extern functions
172-
--> $DIR/variadic-ffi-semantic-restrictions.rs:62:31
154+
--> $DIR/variadic-ffi-semantic-restrictions.rs:59:31
173155
|
174156
LL | fn i_f4(_: ..., x: isize, _: ...) {}
175157
| ^^^^^^
176158
|
177159
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
178160

179-
error: functions cannot be both `const` and C-variadic
180-
--> $DIR/variadic-ffi-semantic-restrictions.rs:65:5
181-
|
182-
LL | const fn i_f5(x: isize, _: ...) {}
183-
| ^^^^^ ^^^^^^ C-variadic because of this
184-
| |
185-
| `const` because of this
186-
187161
error: `...` is not supported for non-extern functions
188-
--> $DIR/variadic-ffi-semantic-restrictions.rs:65:29
162+
--> $DIR/variadic-ffi-semantic-restrictions.rs:62:29
189163
|
190164
LL | const fn i_f5(x: isize, _: ...) {}
191165
| ^^^^^^
192166
|
193167
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
194168

195169
error: `...` is not supported for non-extern functions
196-
--> $DIR/variadic-ffi-semantic-restrictions.rs:72:23
170+
--> $DIR/variadic-ffi-semantic-restrictions.rs:68:23
197171
|
198172
LL | fn t_f1(x: isize, _: ...) {}
199173
| ^^^^^^
200174
|
201175
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
202176

203177
error: `...` is not supported for non-extern functions
204-
--> $DIR/variadic-ffi-semantic-restrictions.rs:74:23
178+
--> $DIR/variadic-ffi-semantic-restrictions.rs:70:23
205179
|
206180
LL | fn t_f2(x: isize, _: ...);
207181
| ^^^^^^
208182
|
209183
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
210184

211185
error: `...` is not supported for non-extern functions
212-
--> $DIR/variadic-ffi-semantic-restrictions.rs:76:13
186+
--> $DIR/variadic-ffi-semantic-restrictions.rs:72:13
213187
|
214188
LL | fn t_f3(_: ...) {}
215189
| ^^^^^^
216190
|
217191
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
218192

219193
error: `...` is not supported for non-extern functions
220-
--> $DIR/variadic-ffi-semantic-restrictions.rs:78:13
194+
--> $DIR/variadic-ffi-semantic-restrictions.rs:74:13
221195
|
222196
LL | fn t_f4(_: ...);
223197
| ^^^^^^
224198
|
225199
= help: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
226200

227201
error: `...` must be the last argument of a C-variadic function
228-
--> $DIR/variadic-ffi-semantic-restrictions.rs:80:13
202+
--> $DIR/variadic-ffi-semantic-restrictions.rs:76:13
229203
|
230204
LL | fn t_f5(_: ..., x: isize) {}
231205
| ^^^^^^
232206

233207
error: `...` must be the last argument of a C-variadic function
234-
--> $DIR/variadic-ffi-semantic-restrictions.rs:82:13
208+
--> $DIR/variadic-ffi-semantic-restrictions.rs:78:13
235209
|
236210
LL | fn t_f6(_: ..., x: isize);
237211
| ^^^^^^
@@ -245,21 +219,21 @@ LL | const unsafe extern "C" fn f4_1(x: isize, _: ...) {}
245219
| the destructor for this type cannot be evaluated in constant functions
246220

247221
error[E0493]: destructor of `VaList<'_>` cannot be evaluated at compile-time
248-
--> $DIR/variadic-ffi-semantic-restrictions.rs:37:36
222+
--> $DIR/variadic-ffi-semantic-restrictions.rs:36:36
249223
|
250224
LL | const extern "C" fn f4_2(x: isize, _: ...) {}
251225
| ^ - value is dropped here
252226
| |
253227
| the destructor for this type cannot be evaluated in constant functions
254228

255229
error[E0493]: destructor of `VaList<'_>` cannot be evaluated at compile-time
256-
--> $DIR/variadic-ffi-semantic-restrictions.rs:65:29
230+
--> $DIR/variadic-ffi-semantic-restrictions.rs:62:29
257231
|
258232
LL | const fn i_f5(x: isize, _: ...) {}
259233
| ^ - value is dropped here
260234
| |
261235
| the destructor for this type cannot be evaluated in constant functions
262236

263-
error: aborting due to 33 previous errors
237+
error: aborting due to 29 previous errors
264238

265239
For more information about this error, try `rustc --explain E0493`.

0 commit comments

Comments
 (0)