Skip to content
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

Rollup of 9 pull requests #128248

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c90b6b8
stabilize `const_int_from_str`
Skgland May 9, 2024
eb799cf
mark `can_not_overflow` as `#[rustc_const_stable(...)]`
Skgland Jul 4, 2024
f99df29
fix tests after rebase
Skgland Jul 4, 2024
404519a
bless tests
Skgland Jul 4, 2024
ceec6dd
update text for E0736 and E0739
folkertdev Jul 16, 2024
4bd3632
improve error message when `#[naked]` is used with `#[inline]`
folkertdev Jul 16, 2024
7e6c083
improve error message when `#[naked]` is used with `#[track-caller] a…
folkertdev Jul 16, 2024
4d082b7
add error message when `#[naked]` is used with `#[test]`
folkertdev Jul 16, 2024
4596ce6
switch to an allowlist approach
folkertdev Jul 17, 2024
0ed639f
merge error codes
folkertdev Jul 17, 2024
6f2318c
allow `#[target_feature]` on `#[naked]` functions
folkertdev Jul 23, 2024
370fcce
rustdoc: change title of search results
lolbinarycat Jul 25, 2024
587b64e
use double quotes
lolbinarycat Jul 25, 2024
caee195
Invert early exit conditions in `collect_tokens_trailing_token`.
nnethercote Jul 25, 2024
4288edb
Inline and remove `AttrWrapper::is_complete`.
nnethercote Jul 25, 2024
3d363c3
Move `is_complete` to the module that uses it.
nnethercote Jul 25, 2024
e631b1e
Invert the sense of `is_complete` and rename it as `needs_tokens`.
nnethercote Jul 25, 2024
a560810
Don't include inner attribute ranges in `CaptureState`.
nnethercote Jul 26, 2024
6e87858
Fix a comment.
nnethercote Jul 26, 2024
6ea2da5
Tweak a loop.
nnethercote Jul 26, 2024
55d37ae
Remove an unnecessary block.
nnethercote Jul 26, 2024
33b98bf
Remove redundant option that was just encoding that a slice was empty
oli-obk Jul 26, 2024
114e0dc
CI: do not respect custom try jobs for unrolled perf builds
Kobzol Jul 26, 2024
33dd288
Add a test case for `extern "C" unsafe` to the ui tests
tdittr Jul 26, 2024
3fdc991
Improve error message for `extern "C" unsafe fn()`
tdittr Jul 26, 2024
130ce49
Fix docs
harryscholes Jul 26, 2024
45e943f
use attribute name that is incompatible in error message
folkertdev Jul 26, 2024
0e50d42
Rollup merge of #124941 - Skgland:stabilize-const-int-from-str, r=dto…
tgross35 Jul 26, 2024
3d85eac
Rollup merge of #127853 - folkertdev:naked-function-error-messages, r…
tgross35 Jul 26, 2024
d4ef544
Rollup merge of #128210 - lolbinarycat:rustdoc-search-title, r=notrid…
tgross35 Jul 26, 2024
d0de531
Rollup merge of #128223 - nnethercote:refactor-collect_tokens, r=petr…
tgross35 Jul 26, 2024
97f49e1
Rollup merge of #128224 - nnethercote:fewer-replace_ranges, r=petroch…
tgross35 Jul 26, 2024
436d6aa
Rollup merge of #128226 - oli-obk:option_vs_empty_slice, r=petrochenkov
tgross35 Jul 26, 2024
caa64a5
Rollup merge of #128227 - Kobzol:ci-unrolled-perf-build-matrix, r=tgr…
tgross35 Jul 26, 2024
6257615
Rollup merge of #128229 - tdittr:unsafe-extern-abi-error, r=compiler-…
tgross35 Jul 26, 2024
2157c39
Rollup merge of #128235 - harryscholes:fix-iterator-filter-docs, r=tg…
tgross35 Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve error message when #[naked] is used with #[inline]
  • Loading branch information
folkertdev committed Jul 16, 2024
commit 4bd36324b6a19afdbcb53de1d41e65b0060a6dbb
11 changes: 5 additions & 6 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ passes_break_non_loop =
.suggestion = use `break` on its own without a value inside this `{$kind}` loop
.break_expr_suggestion = alternatively, you might have meant to use the available loop label

passes_cannot_inline_naked_function =
naked functions cannot be inlined

passes_cannot_stabilize_deprecated =
an API can't be stabilized after it is deprecated
.label = invalid version
Expand Down Expand Up @@ -485,16 +482,18 @@ passes_naked_functions_asm_block =
passes_naked_functions_asm_options =
asm options unsupported in naked functions: {$unsupported_options}

passes_naked_functions_codegen_attribute =
cannot use additional code generation attributes with `#[naked]`
.label = this attribute is incompatible with `#[naked]`
.label2 = function marked with `#[naked]` here

passes_naked_functions_must_use_noreturn =
asm in naked functions must use `noreturn` option
.suggestion = consider specifying that the asm block is responsible for returning from the function

passes_naked_functions_operands =
only `const` and `sym` operands are supported in naked functions

passes_naked_tracked_caller =
cannot use `#[track_caller]` with `#[naked]`

passes_no_link =
attribute should be applied to an `extern crate` item
.label = not an `extern crate` item
Expand Down
33 changes: 25 additions & 8 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::rustc_std_internal_symbol] => {
self.check_rustc_std_internal_symbol(attr, span, target)
}
[sym::naked] => self.check_naked(hir_id, attr, span, target),
[sym::naked] => self.check_naked(hir_id, attr, span, target, attrs),
[sym::rustc_never_returns_null_ptr] => {
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
}
Expand Down Expand Up @@ -410,12 +410,33 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}

/// Checks if `#[naked]` is applied to a function definition.
fn check_naked(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
fn check_naked(
&self,
hir_id: HirId,
attr: &Attribute,
span: Span,
target: Target,
attrs: &[Attribute],
) -> bool {
const FORBIDDEN: [rustc_span::Symbol; 3] =
[sym::track_caller, sym::inline, sym::target_feature];

for other_attr in attrs {
if FORBIDDEN.into_iter().any(|name| other_attr.has_name(name)) {
self.dcx().emit_err(errors::NakedFunctionCodegenAttribute {
span: other_attr.span,
naked_span: attr.span,
});

return false;
}
}

match target {
Target::Fn
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
// `#[allow_internal_unstable]` attribute with just a lint, because we previously
// `#[naked]` attribute with just a lint, because we previously
// erroneously allowed it and some crates used it accidentally, to be compatible
// with crates depending on them, we can't throw an error here.
Target::Field | Target::Arm | Target::MacroDef => {
Expand Down Expand Up @@ -488,7 +509,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}

/// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
/// Checks if a `#[track_caller]` is applied to a function. Returns `true` if valid.
fn check_track_caller(
&self,
hir_id: HirId,
Expand All @@ -498,10 +519,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
target: Target,
) -> bool {
match target {
_ if attrs.iter().any(|attr| attr.has_name(sym::naked)) => {
self.dcx().emit_err(errors::NakedTrackedCaller { attr_span });
false
}
Target::Fn => {
// `#[track_caller]` is not valid on weak lang items because they are called via
// `extern` declarations and `#[track_caller]` would alter their ABI.
Expand Down
24 changes: 10 additions & 14 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ pub struct AttrShouldBeAppliedToFn {
pub on_crate: bool,
}

#[derive(Diagnostic)]
#[diag(passes_naked_tracked_caller, code = E0736)]
pub struct NakedTrackedCaller {
#[primary_span]
pub attr_span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_should_be_applied_to_fn, code = E0739)]
pub struct TrackedCallerWrongLocation {
Expand Down Expand Up @@ -1124,13 +1117,6 @@ pub struct UnlabeledCfInWhileCondition<'a> {
pub cf_type: &'a str,
}

#[derive(Diagnostic)]
#[diag(passes_cannot_inline_naked_function)]
pub struct CannotInlineNakedFunction {
#[primary_span]
pub span: Span,
}

#[derive(LintDiagnostic)]
#[diag(passes_undefined_naked_function_abi)]
pub struct UndefinedNakedFunctionAbi;
Expand Down Expand Up @@ -1196,6 +1182,16 @@ pub struct NakedFunctionsMustUseNoreturn {
pub last_span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_naked_functions_codegen_attribute, code = E0736)]
pub struct NakedFunctionCodegenAttribute {
#[primary_span]
#[label]
pub span: Span,
#[label(passes_label2)]
pub naked_span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_attr_only_in_functions)]
pub struct AttrOnlyInFunctions {
Expand Down
14 changes: 2 additions & 12 deletions compiler/rustc_passes/src/naked_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use rustc_span::Span;
use rustc_target::spec::abi::Abi;

use crate::errors::{
CannotInlineNakedFunction, NakedFunctionsAsmBlock, NakedFunctionsAsmOptions,
NakedFunctionsMustUseNoreturn, NakedFunctionsOperands, NoPatterns, ParamsNotAllowed,
UndefinedNakedFunctionAbi,
NakedFunctionsAsmBlock, NakedFunctionsAsmOptions, NakedFunctionsMustUseNoreturn,
NakedFunctionsOperands, NoPatterns, ParamsNotAllowed, UndefinedNakedFunctionAbi,
};

pub(crate) fn provide(providers: &mut Providers) {
Expand Down Expand Up @@ -53,15 +52,6 @@ fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
check_no_patterns(tcx, body.params);
check_no_parameters_use(tcx, body);
check_asm(tcx, def_id, body);
check_inline(tcx, def_id);
}
}

/// Check that the function isn't inlined.
fn check_inline(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let attrs = tcx.get_attrs(def_id, sym::inline);
for attr in attrs {
tcx.dcx().emit_err(CannotInlineNakedFunction { span: attr.span });
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/ui/asm/naked-functions-inline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//@ needs-asm-support
#![feature(naked_functions)]
#![crate_type = "lib"]

use std::arch::asm;

#[naked]
pub unsafe extern "C" fn inline_none() {
asm!("", options(noreturn));
}

#[naked]
#[inline]
//~^ ERROR [E0736]
pub unsafe extern "C" fn inline_hint() {
asm!("", options(noreturn));
}

#[naked]
#[inline(always)]
//~^ ERROR [E0736]
pub unsafe extern "C" fn inline_always() {
asm!("", options(noreturn));
}

#[naked]
#[inline(never)]
//~^ ERROR [E0736]
pub unsafe extern "C" fn inline_never() {
asm!("", options(noreturn));
}
27 changes: 27 additions & 0 deletions tests/ui/asm/naked-functions-inline.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0736]: cannot use additional code generation attributes with `#[naked]`
--> $DIR/naked-functions-inline.rs:13:1
|
LL | #[naked]
| -------- function marked with `#[naked]` here
LL | #[inline]
| ^^^^^^^^^ this attribute is incompatible with `#[naked]`

error[E0736]: cannot use additional code generation attributes with `#[naked]`
--> $DIR/naked-functions-inline.rs:20:1
|
LL | #[naked]
| -------- function marked with `#[naked]` here
LL | #[inline(always)]
| ^^^^^^^^^^^^^^^^^ this attribute is incompatible with `#[naked]`

error[E0736]: cannot use additional code generation attributes with `#[naked]`
--> $DIR/naked-functions-inline.rs:27:1
|
LL | #[naked]
| -------- function marked with `#[naked]` here
LL | #[inline(never)]
| ^^^^^^^^^^^^^^^^ this attribute is incompatible with `#[naked]`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0736`.
31 changes: 0 additions & 31 deletions tests/ui/asm/naked-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,37 +168,6 @@ pub unsafe extern "C" fn inline_none() {
}

#[naked]
#[inline]
//~^ ERROR naked functions cannot be inlined
pub unsafe extern "C" fn inline_hint() {
asm!("", options(noreturn));
}

#[naked]
#[inline(always)]
//~^ ERROR naked functions cannot be inlined
pub unsafe extern "C" fn inline_always() {
asm!("", options(noreturn));
}

#[naked]
#[inline(never)]
//~^ ERROR naked functions cannot be inlined
pub unsafe extern "C" fn inline_never() {
asm!("", options(noreturn));
}

#[naked]
#[inline]
//~^ ERROR naked functions cannot be inlined
#[inline(always)]
//~^ ERROR naked functions cannot be inlined
#[inline(never)]
//~^ ERROR naked functions cannot be inlined
pub unsafe extern "C" fn inline_all() {
asm!("", options(noreturn));
}

#[naked]
pub unsafe extern "C" fn allow_compile_error(a: u32) -> u32 {
compile_error!("this is a user specified error")
Expand Down
44 changes: 4 additions & 40 deletions tests/ui/asm/naked-functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ LL | asm!("", options(readonly, nostack), options(pure));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^

error: this is a user specified error
--> $DIR/naked-functions.rs:204:5
--> $DIR/naked-functions.rs:173:5
|
LL | compile_error!("this is a user specified error")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: this is a user specified error
--> $DIR/naked-functions.rs:210:5
--> $DIR/naked-functions.rs:179:5
|
LL | compile_error!("this is a user specified error");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: asm template must be a string literal
--> $DIR/naked-functions.rs:217:10
--> $DIR/naked-functions.rs:186:10
|
LL | asm!(invalid_syntax)
| ^^^^^^^^^^^^^^
Expand Down Expand Up @@ -249,42 +249,6 @@ warning: Rust ABI is unsupported in naked functions
LL | pub unsafe fn rust_abi() {
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: naked functions cannot be inlined
--> $DIR/naked-functions.rs:171:1
|
LL | #[inline]
| ^^^^^^^^^

error: naked functions cannot be inlined
--> $DIR/naked-functions.rs:178:1
|
LL | #[inline(always)]
| ^^^^^^^^^^^^^^^^^

error: naked functions cannot be inlined
--> $DIR/naked-functions.rs:185:1
|
LL | #[inline(never)]
| ^^^^^^^^^^^^^^^^

error: naked functions cannot be inlined
--> $DIR/naked-functions.rs:192:1
|
LL | #[inline]
| ^^^^^^^^^

error: naked functions cannot be inlined
--> $DIR/naked-functions.rs:194:1
|
LL | #[inline(always)]
| ^^^^^^^^^^^^^^^^^

error: naked functions cannot be inlined
--> $DIR/naked-functions.rs:196:1
|
LL | #[inline(never)]
| ^^^^^^^^^^^^^^^^

error: aborting due to 33 previous errors; 2 warnings emitted
error: aborting due to 27 previous errors; 2 warnings emitted

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