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 11 pull requests #126134

Merged
merged 39 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d5d56d0
Stabilize `BinaryHeap::as_slice`
slanterns Apr 16, 2024
a272f8e
Update `./x fmt` command
ChrisDenton Jun 5, 2024
35130d7
Detect pub structs never constructed and unused associated constants …
mu001999 Jun 5, 2024
c0e2543
Simplify the rayon calls in the installer
cuviper Jun 5, 2024
95b4c07
Reduce `pub` exposure.
nnethercote Jun 3, 2024
13f2bc9
Specialize `assert_pred`.
nnethercote Jun 4, 2024
4c731c2
Inline and remove `check_builtin_attribute`.
nnethercote Jun 4, 2024
72cbb8b
Revert "Use the HIR instead of mir_keys for determining whether somet…
oli-obk Jun 6, 2024
c76e59e
prefer compile::stream_cargo for building tools
onur-ozkan Jun 6, 2024
26dc8bd
Stabilize Option::take_if
wutchzone Jun 6, 2024
67f5dd1
Parse unsafe attributes
carbotaniuman Apr 21, 2024
c4de986
Disallow unsafe in derive
carbotaniuman Apr 23, 2024
6ec492f
Reorder `unsafe` macro cases
carbotaniuman Apr 25, 2024
48851d9
Add tests
carbotaniuman Apr 28, 2024
230b58f
Error on unsafe on non-unsafe attribute
carbotaniuman May 15, 2024
68719e0
Fix doc comment
carbotaniuman May 16, 2024
b82c524
Add double unsafe test
carbotaniuman May 16, 2024
87be1ba
Fix build
carbotaniuman May 21, 2024
8aa2553
Change comment to FIXME
carbotaniuman May 27, 2024
15f8ffe
Fix formatting
carbotaniuman May 27, 2024
1cc4e8c
Add unsafe to `ffi_pure` and `ffi_const`
carbotaniuman May 27, 2024
453093b
Remove extraneous attribute
carbotaniuman Jun 7, 2024
f83cdbd
Fix ordering
carbotaniuman Jun 7, 2024
f9104f0
Handle safe case
carbotaniuman Jun 7, 2024
7eff2d9
Do not pass source root when building run-make tests
Kobzol Jun 7, 2024
eac63b7
Rename `S` environment variable to `SOURCE_ROOT` in run-make tests
Kobzol Jun 7, 2024
3eb5341
Improve docs for using custom paths with `--emit`
Zalathar Jun 7, 2024
9461852
Add regression test
oli-obk Jun 7, 2024
0acb5b8
Rollup merge of #124012 - slanterns:as_slice_stabilize, r=BurntSushi
matthiaskrgr Jun 7, 2024
6e534c7
Rollup merge of #124214 - carbotaniuman:parse_unsafe_attrs, r=michael…
matthiaskrgr Jun 7, 2024
13314df
Rollup merge of #125572 - mu001999-contrib:dead/enhance, r=pnkfelix
matthiaskrgr Jun 7, 2024
65fcba6
Rollup merge of #125781 - onur-ozkan:improve-tool-builder, r=albertla…
matthiaskrgr Jun 7, 2024
3689adf
Rollup merge of #126030 - ChrisDenton:update-wingen-readme, r=Mark-Si…
matthiaskrgr Jun 7, 2024
bc79329
Rollup merge of #126047 - cuviper:installer-try_for_each, r=albertlar…
matthiaskrgr Jun 7, 2024
4aceaaa
Rollup merge of #126052 - nnethercote:rustc_parse-more-cleanups, r=sp…
matthiaskrgr Jun 7, 2024
2e82d7f
Rollup merge of #126077 - oli-obk:revert_is_mir_available, r=BoxyUwU
matthiaskrgr Jun 7, 2024
ccbd6c2
Rollup merge of #126089 - wutchzone:option_take_if, r=scottmcm
matthiaskrgr Jun 7, 2024
b8e28d1
Rollup merge of #126112 - Kobzol:runmake-source-root, r=jieyouxu
matthiaskrgr Jun 7, 2024
df35d7a
Rollup merge of #126119 - Zalathar:emit-filename, r=ehuss
matthiaskrgr Jun 7, 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
Error on unsafe on non-unsafe attribute
  • Loading branch information
carbotaniuman committed Jun 7, 2024
commit 230b58febf78bb1680b4960d32350a7430a8aab5
4 changes: 4 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,10 @@ pub fn is_valid_for_get_attr(name: Symbol) -> bool {
})
}

pub fn is_unsafe_attr(name: Symbol) -> bool {
BUILTIN_ATTRIBUTE_MAP.get(&name).is_some_and(|attr| attr.safety == AttributeSafety::Unsafe)
}

pub static BUILTIN_ATTRIBUTE_MAP: LazyLock<FxHashMap<Symbol, &BuiltinAttribute>> =
LazyLock::new(|| {
let mut map = FxHashMap::default();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_feature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ pub use accepted::ACCEPTED_FEATURES;
pub use builtin_attrs::AttributeDuplicates;
pub use builtin_attrs::{
deprecated_attributes, encode_cross_crate, find_gated_cfg, is_builtin_attr_name,
is_valid_for_get_attr, AttributeGate, AttributeTemplate, AttributeType, BuiltinAttribute,
GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
is_unsafe_attr, is_valid_for_get_attr, AttributeGate, AttributeTemplate, AttributeType,
BuiltinAttribute, GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
};
pub use removed::REMOVED_FEATURES;
pub use unstable::{Features, INCOMPATIBLE_FEATURES, UNSTABLE_FEATURES};
4 changes: 4 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ passes_invalid_attr_at_crate_level =
passes_invalid_attr_at_crate_level_item =
the inner attribute doesn't annotate this {$kind}

passes_invalid_attr_unsafe = `{$name}` is not an unsafe attribute
.suggestion = remove the `unsafe(...)`
.note = extraneous unsafe is not allowed in attributes

passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export]` argument

passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments
Expand Down
22 changes: 21 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use rustc_ast::{MetaItemKind, MetaItemLit, NestedMetaItem};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::StashKey;
use rustc_errors::{Applicability, DiagCtxt, IntoDiagArg, MultiSpan};
use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
use rustc_feature::{
is_unsafe_attr, AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP,
};
use rustc_hir::def_id::LocalModDefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{self as hir};
Expand Down Expand Up @@ -114,6 +116,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
let mut seen = FxHashMap::default();
let attrs = self.tcx.hir().attrs(hir_id);
for attr in attrs {
self.check_unsafe_attr(attr);

match attr.path().as_slice() {
[sym::diagnostic, sym::do_not_recommend] => {
self.check_do_not_recommend(attr.span, hir_id, target)
Expand Down Expand Up @@ -308,6 +312,22 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
true
}


/// Checks if `unsafe()` is applied to an invalid attribute.
fn check_unsafe_attr(&self, attr: &Attribute) {
if !attr.is_doc_comment() {
let attr_item = attr.get_normal_item();
if let ast::Unsafe::Yes(unsafe_span) = attr_item.unsafety {
if !is_unsafe_attr(attr.name_or_empty()) {
self.dcx().emit_err(errors::InvalidAttrUnsafe {
span: unsafe_span,
name: attr_item.path.clone(),
});
}
}
}
}

/// Checks if `#[diagnostic::on_unimplemented]` is applied to a trait definition
fn check_diagnostic_on_unimplemented(
&self,
Expand Down
11 changes: 10 additions & 1 deletion compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use crate::fluent_generated as fluent;
use rustc_ast::Label;
use rustc_ast::{ast, Label};
use rustc_errors::{
codes::*, Applicability, Diag, DiagCtxt, DiagSymbolList, Diagnostic, EmissionGuarantee, Level,
MultiSpan, SubdiagMessageOp, Subdiagnostic,
Expand Down Expand Up @@ -863,6 +863,15 @@ pub struct InvalidAttrAtCrateLevel {
pub item: Option<ItemFollowingInnerAttr>,
}

#[derive(Diagnostic)]
#[diag(passes_invalid_attr_unsafe)]
#[note]
pub struct InvalidAttrUnsafe {
#[primary_span]
pub span: Span,
pub name: ast::Path,
}

#[derive(Clone, Copy)]
pub struct ItemFollowingInnerAttr {
pub span: Span,
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/attributes/unsafe/unsafe-safe-attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(unsafe_attributes)]

#[unsafe(repr(C))] //~ ERROR: is not an unsafe attribute
struct Foo {}

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/attributes/unsafe/unsafe-safe-attribute.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: `repr` is not an unsafe attribute
--> $DIR/unsafe-safe-attribute.rs:3:3
|
LL | #[unsafe(repr(C))]
| ^^^^^^
|
= note: extraneous unsafe is not allowed in attributes

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(unsafe_attributes)]

#[unsafe(diagnostic::on_unimplemented( //~ ERROR: is not an unsafe attribute
message = "testing",
))]
trait Foo {}

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/attributes/unsafe/unsafe-safe-attribute_diagnostic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: `diagnostic::on_unimplemented` is not an unsafe attribute
--> $DIR/unsafe-safe-attribute_diagnostic.rs:3:3
|
LL | #[unsafe(diagnostic::on_unimplemented(
| ^^^^^^
|
= note: extraneous unsafe is not allowed in attributes

error: aborting due to 1 previous error