-
Notifications
You must be signed in to change notification settings - Fork 13.5k
improve c-variadic errors #143546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
folkertdev
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
folkertdev:c-variadic-improve-errors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+583
−199
Open
improve c-variadic errors #143546
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -460,7 +460,7 @@ impl<'a> AstValidator<'a> { | |
} | ||
|
||
if !spans.is_empty() { | ||
let header_span = sig.header.span().unwrap_or(sig.span.shrink_to_lo()); | ||
let header_span = sig.header_span(); | ||
let suggestion_span = header_span.shrink_to_hi().to(sig.decl.output.span()); | ||
let padding = if header_span.is_empty() { "" } else { " " }; | ||
|
||
|
@@ -633,46 +633,73 @@ impl<'a> AstValidator<'a> { | |
/// - Non-const | ||
/// - Either foreign, or free and `unsafe extern "C"` semantically | ||
fn check_c_variadic_type(&self, fk: FnKind<'a>) { | ||
let variadic_spans: Vec<_> = fk | ||
.decl() | ||
.inputs | ||
.iter() | ||
.filter(|arg| matches!(arg.ty.kind, TyKind::CVarArgs)) | ||
.map(|arg| arg.span) | ||
.collect(); | ||
let variadic_params: Vec<_> = | ||
fk.decl().inputs.iter().filter(|arg| matches!(arg.ty.kind, TyKind::CVarArgs)).collect(); | ||
|
||
if variadic_spans.is_empty() { | ||
// The parser already rejects `...` if it's not the final argument, but we still want to | ||
// emit the errors below, so we only consider the final `...` here. | ||
let Some(variadic_param) = variadic_params.last() else { | ||
return; | ||
}; | ||
|
||
let FnKind::Fn(fn_ctxt, _, Fn { sig, .. }) = fk else { | ||
// Unreachable because the parser already rejects `...` in closures. | ||
unreachable!("C variable argument list cannot be used in closures") | ||
}; | ||
|
||
// C-variadics are not yet implemented in const evaluation. | ||
if let Const::Yes(const_span) = sig.header.constness { | ||
self.dcx().emit_err(errors::ConstAndCVariadic { | ||
span: const_span.to(variadic_param.span), | ||
const_span, | ||
variadic_span: variadic_param.span, | ||
}); | ||
} | ||
|
||
if let Some(header) = fk.header() { | ||
if let Const::Yes(const_span) = header.constness { | ||
let mut spans = variadic_spans.clone(); | ||
spans.push(const_span); | ||
self.dcx().emit_err(errors::ConstAndCVariadic { | ||
spans, | ||
const_span, | ||
variadic_spans: variadic_spans.clone(), | ||
}); | ||
} | ||
if let Some(coroutine_kind) = sig.header.coroutine_kind { | ||
self.dcx().emit_err(errors::CoroutineAndCVariadic { | ||
span: coroutine_kind.span().to(variadic_param.span), | ||
coroutine_kind: coroutine_kind.as_str(), | ||
coroutine_span: coroutine_kind.span(), | ||
variadic_span: variadic_param.span, | ||
}); | ||
} | ||
Comment on lines
+659
to
666
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this fixes #125431 by just making it illegal, which is neat. |
||
|
||
match (fk.ctxt(), fk.header()) { | ||
(Some(FnCtxt::Foreign), _) => return, | ||
(Some(FnCtxt::Free), Some(header)) => match header.ext { | ||
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }, _) | ||
| Extern::Explicit(StrLit { symbol_unescaped: sym::C_dash_unwind, .. }, _) | ||
| Extern::Implicit(_) | ||
if matches!(header.safety, Safety::Unsafe(_)) => | ||
{ | ||
return; | ||
} | ||
_ => {} | ||
}, | ||
_ => {} | ||
}; | ||
match fn_ctxt { | ||
FnCtxt::Free => { | ||
match sig.header.ext { | ||
Extern::Implicit(_) => { /* defaults to "C" */ } | ||
Extern::Explicit(StrLit { symbol_unescaped, .. }, _) => { | ||
if !matches!(symbol_unescaped, sym::C | sym::C_dash_unwind) { | ||
self.dcx().emit_err(errors::CVariadicBadExtern { | ||
span: variadic_param.span, | ||
abi: symbol_unescaped, | ||
extern_span: sig.extern_span(), | ||
}); | ||
} | ||
} | ||
Extern::None => { | ||
self.dcx() | ||
.emit_err(errors::CVariadicNoExtern { span: variadic_param.span }); | ||
} | ||
}; | ||
|
||
self.dcx().emit_err(errors::BadCVariadic { span: variadic_spans }); | ||
if !matches!(sig.header.safety, Safety::Unsafe(_)) { | ||
self.dcx().emit_err(errors::CVariadicMustBeUnsafe { | ||
span: variadic_param.span, | ||
unsafe_span: sig.safety_span(), | ||
}); | ||
} | ||
} | ||
FnCtxt::Assoc(_) => { | ||
// For now, C variable argument lists are unsupported in associated functions. | ||
self.dcx() | ||
.emit_err(errors::CVariadicAssociatedFunction { span: variadic_param.span }); | ||
} | ||
FnCtxt::Foreign => { | ||
// Whether the ABI supports C variable argument lists is checked later. | ||
} | ||
} | ||
} | ||
|
||
fn check_item_named(&self, ident: Ident, kind: &str) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.