Skip to content

Commit

Permalink
De-fatalize ... parsing.
Browse files Browse the repository at this point in the history
Also fix error the code description.
  • Loading branch information
Centril committed Dec 12, 2019
1 parent e52f902 commit 74d4fbc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
16 changes: 11 additions & 5 deletions src/librustc_error_codes/error_codes/E0743.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
C-variadic has been used on a non-foreign function.
The C-variadic type `...` has been nested inside another type.

Erroneous code example:

```compile_fail,E0743
fn foo2(x: u8, ...) {} // error!
#![feature(c_variadic)]
fn foo2(x: u8, y: &...) {} // error!
```

Only foreign functions can use C-variadic (`...`). It is used to give an
undefined number of parameters to a given function (like `printf` in C). The
equivalent in Rust would be to use macros directly.
Only foreign functions can use the C-variadic type (`...`).
In such functions, `...` may only occur non-nested.
That is, `y: &'a ...` is not allowed.

A C-variadic type is used to give an undefined number
of parameters to a given function (like `printf` in C).
The equivalent in Rust would be to use macros directly.
16 changes: 9 additions & 7 deletions src/librustc_parse/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use syntax::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam
use syntax::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef};
use syntax::ast::{Mutability, AnonConst, Mac};
use syntax::token::{self, Token};
use syntax::struct_span_fatal;
use syntax::struct_span_err;
use syntax_pos::source_map::Span;
use syntax_pos::symbol::kw;

Expand Down Expand Up @@ -209,19 +209,21 @@ impl<'a> Parser<'a> {
TyKind::Path(None, path)
}
}
} else if self.check(&token::DotDotDot) {
} else if self.eat(&token::DotDotDot) {
if allow_c_variadic {
self.eat(&token::DotDotDot);
TyKind::CVarArgs
} else {
// FIXME(Centril): Should we just allow `...` syntactically
// anywhere in a type and use semantic restrictions instead?
return Err(struct_span_fatal!(
struct_span_err!(
self.sess.span_diagnostic,
self.token.span,
lo.to(self.prev_span),
E0743,
"only foreign functions are allowed to be C-variadic",
));
"C-variadic type `...` may not be nested inside another type",
)
.emit();

TyKind::Err
}
} else {
let msg = format!("expected type, found {}", self.this_token_descr());
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/parser/variadic-ffi-nested-syntactic-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn f1<'a>(x: u8, y: &'a ...) {}
//~^ ERROR C-variadic type `...` may not be nested inside another type

fn f2<'a>(x: u8, y: Vec<&'a ...>) {}
//~^ ERROR C-variadic type `...` may not be nested inside another type

fn main() {
let _recovery_witness: () = 0; //~ ERROR mismatched types
}
24 changes: 24 additions & 0 deletions src/test/ui/parser/variadic-ffi-nested-syntactic-fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0743]: C-variadic type `...` may not be nested inside another type
--> $DIR/variadic-ffi-nested-syntactic-fail.rs:1:25
|
LL | fn f1<'a>(x: u8, y: &'a ...) {}
| ^^^

error[E0743]: C-variadic type `...` may not be nested inside another type
--> $DIR/variadic-ffi-nested-syntactic-fail.rs:4:29
|
LL | fn f2<'a>(x: u8, y: Vec<&'a ...>) {}
| ^^^

error[E0308]: mismatched types
--> $DIR/variadic-ffi-nested-syntactic-fail.rs:8:33
|
LL | let _recovery_witness: () = 0;
| -- ^ expected `()`, found integer
| |
| expected due to this

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0308, E0743.
For more information about an error, try `rustc --explain E0308`.

0 comments on commit 74d4fbc

Please sign in to comment.