Skip to content

Commit 2c243d9

Browse files
committed
Auto merge of #126891 - matthiaskrgr:rollup-p6dl1gk, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #126177 (Add hard error and migration lint for unsafe attrs) - #126298 (Promote loongarch64-unknown-linux-musl to Tier 2 with host tools) - #126455 (For [E0308]: mismatched types, when expr is in an arm's body, not add semicolon ';' at the end of it.) - #126754 (Implement `use<>` formatting in rustfmt) - #126807 (std::unix::fs: copy simplification for apple.) - #126845 (Weekly `cargo update`) - #126849 (Fix 32-bit Arm reg classes by hierarchically sorting them) - #126854 (std::unix::os::home_dir: fallback's optimisation.) - #126888 (Remove stray println from rustfmt's `rewrite_static`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents d49994b + b94d275 commit 2c243d9

48 files changed

Lines changed: 987 additions & 280 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 57 additions & 128 deletions
Large diffs are not rendered by default.

‎compiler/rustc_ast_passes/src/ast_validation.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara
899899

900900
impl<'a>Visitor<'a>forAstValidator<'a>{
901901
fnvisit_attribute(&mutself,attr:&Attribute){
902-
validate_attr::check_attr(&self.session.psess, attr);
902+
validate_attr::check_attr(&self.features,&self.session.psess, attr);
903903
}
904904

905905
fnvisit_ty(&mutself,ty:&'aTy){

‎compiler/rustc_expand/src/expand.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
18831883
letmut span:Option<Span> = None;
18841884
whileletSome(attr) = attrs.next(){
18851885
rustc_ast_passes::feature_gate::check_attribute(attr,self.cx.sess, features);
1886-
validate_attr::check_attr(&self.cx.sess.psess, attr);
1886+
validate_attr::check_attr(features,&self.cx.sess.psess, attr);
18871887

18881888
let current_span = ifletSome(sp) = span { sp.to(attr.span)}else{ attr.span};
18891889
span = Some(current_span);

‎compiler/rustc_feature/src/builtin_attrs.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,10 +1145,6 @@ pub fn is_valid_for_get_attr(name: Symbol) -> bool {
11451145
})
11461146
}
11471147

1148-
pubfnis_unsafe_attr(name:Symbol) -> bool{
1149-
BUILTIN_ATTRIBUTE_MAP.get(&name).is_some_and(|attr| attr.safety == AttributeSafety::Unsafe)
1150-
}
1151-
11521148
pubstaticBUILTIN_ATTRIBUTE_MAP:LazyLock<FxHashMap<Symbol,&BuiltinAttribute>> =
11531149
LazyLock::new(|| {
11541150
letmut map = FxHashMap::default();

‎compiler/rustc_feature/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub use accepted::ACCEPTED_FEATURES;
125125
pubuse builtin_attrs::AttributeDuplicates;
126126
pubuse builtin_attrs::{
127127
deprecated_attributes, encode_cross_crate, find_gated_cfg, is_builtin_attr_name,
128-
is_unsafe_attr,is_valid_for_get_attr,AttributeGate,AttributeTemplate,AttributeType,
128+
is_valid_for_get_attr,AttributeGate,AttributeSafety,AttributeTemplate,AttributeType,
129129
BuiltinAttribute,GatedCfg,BUILTIN_ATTRIBUTES,BUILTIN_ATTRIBUTE_MAP,
130130
};
131131
pubuse removed::REMOVED_FEATURES;

‎compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs‎

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ use rustc_middle::lint::in_external_macro;
2626
use rustc_middle::middle::stability::EvalResult;
2727
use rustc_middle::span_bug;
2828
use rustc_middle::ty::print::with_no_trimmed_paths;
29-
use rustc_middle::ty::{
30-
self, suggest_constraining_type_params,Article,Binder,IsSuggestable,Ty,TypeVisitableExt,
31-
Upcast,
32-
};
29+
use rustc_middle::ty::{self, suggest_constraining_type_params,Article,Binder};
30+
use rustc_middle::ty::{IsSuggestable,Ty,TyCtxt,TypeVisitableExt,Upcast};
3331
use rustc_session::errors::ExprParenthesesNeeded;
3432
use rustc_span::source_map::Spanned;
3533
use rustc_span::symbol::{sym,Ident};
@@ -1111,12 +1109,56 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11111109
self.tcx.hir().span_with_body(self.tcx.local_def_id_to_hir_id(fn_id)),
11121110
)
11131111
{
1114-
err.multipart_suggestion(
1112+
// When the expr is in a match arm's body, we shouldn't add semicolon ';' at the end.
1113+
// For example:
1114+
// fn mismatch_types() -> i32 {
1115+
// match 1 {
1116+
// x => dbg!(x),
1117+
// }
1118+
// todo!()
1119+
// }
1120+
// -------------^^^^^^^-
1121+
// Don't add semicolon `;` at the end of `dbg!(x)` expr
1122+
fnis_in_arm<'tcx>(expr:&'tcx hir::Expr<'tcx>,tcx:TyCtxt<'tcx>) -> bool{
1123+
for(_, node)in tcx.hir().parent_iter(expr.hir_id){
1124+
match node {
1125+
hir::Node::Block(block) => {
1126+
ifletSome(ret) = block.expr
1127+
&& ret.hir_id == expr.hir_id
1128+
{
1129+
continue;
1130+
}
1131+
}
1132+
hir::Node::Arm(arm) => {
1133+
iflet hir::ExprKind::Block(block, _) = arm.body.kind
1134+
&& letSome(ret) = block.expr
1135+
&& ret.hir_id == expr.hir_id
1136+
{
1137+
returntrue;
1138+
}
1139+
}
1140+
hir::Node::Expr(e)iflet hir::ExprKind::Block(block, _) = e.kind => {
1141+
ifletSome(ret) = block.expr
1142+
&& ret.hir_id == expr.hir_id
1143+
{
1144+
continue;
1145+
}
1146+
}
1147+
_ => {
1148+
returnfalse;
1149+
}
1150+
}
1151+
}
1152+
1153+
false
1154+
}
1155+
letmut suggs = vec![(span.shrink_to_lo(),"return ".to_string())];
1156+
if !is_in_arm(expr,self.tcx){
1157+
suggs.push((span.shrink_to_hi(),";".to_string()));
1158+
}
1159+
err.multipart_suggestion_verbose(
11151160
"you might have meant to return this value",
1116-
vec![
1117-
(span.shrink_to_lo(),"return ".to_string()),
1118-
(span.shrink_to_hi(),";".to_string()),
1119-
],
1161+
suggs,
11201162
Applicability::MaybeIncorrect,
11211163
);
11221164
}

‎compiler/rustc_lint/messages.ftl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,10 @@ lint_unnameable_test_items = cannot test inner items
825825
lint_unnecessary_qualification = unnecessary qualification
826826
.suggestion = remove the unnecessary path segments
827827
828+
lint_unsafe_attr_outside_unsafe = unsafe attribute used without unsafe
829+
.label = usage of unsafe attribute
830+
lint_unsafe_attr_outside_unsafe_suggestion = wrap the attribute in `unsafe(...)`
831+
828832
lint_unsupported_group = `{$lint_group}` lint group is not supported with ´--force-warn´
829833
830834
lint_untranslatable_diag = diagnostics should be created using translatable messages

‎compiler/rustc_lint/src/context/diagnostics.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
319319
BuiltinLintDiag::UnusedQualifications{ removal_span } => {
320320
lints::UnusedQualifications{ removal_span }.decorate_lint(diag);
321321
}
322+
BuiltinLintDiag::UnsafeAttrOutsideUnsafe{
323+
attribute_name_span,
324+
sugg_spans:(left, right),
325+
} => {
326+
lints::UnsafeAttrOutsideUnsafe{
327+
span: attribute_name_span,
328+
suggestion: lints::UnsafeAttrOutsideUnsafeSuggestion{ left, right },
329+
}
330+
.decorate_lint(diag);
331+
}
322332
BuiltinLintDiag::AssociatedConstElidedLifetime{
323333
elided,
324334
span: lt_span,

‎compiler/rustc_lint/src/lints.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,3 +2890,24 @@ pub struct RedundantImportVisibility {
28902890
pubimport_vis:String,
28912891
pubmax_vis:String,
28922892
}
2893+
2894+
#[derive(LintDiagnostic)]
2895+
#[diag(lint_unsafe_attr_outside_unsafe)]
2896+
pubstructUnsafeAttrOutsideUnsafe{
2897+
#[label]
2898+
pubspan:Span,
2899+
#[subdiagnostic]
2900+
pubsuggestion:UnsafeAttrOutsideUnsafeSuggestion,
2901+
}
2902+
2903+
#[derive(Subdiagnostic)]
2904+
#[multipart_suggestion(
2905+
lint_unsafe_attr_outside_unsafe_suggestion,
2906+
applicability = "machine-applicable"
2907+
)]
2908+
pubstructUnsafeAttrOutsideUnsafeSuggestion{
2909+
#[suggestion_part(code = "unsafe(")]
2910+
publeft:Span,
2911+
#[suggestion_part(code = ")")]
2912+
pubright:Span,
2913+
}

‎compiler/rustc_lint_defs/src/builtin.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ declare_lint_pass! {
115115
UNNAMEABLE_TYPES,
116116
UNREACHABLE_CODE,
117117
UNREACHABLE_PATTERNS,
118+
UNSAFE_ATTR_OUTSIDE_UNSAFE,
118119
UNSAFE_OP_IN_UNSAFE_FN,
119120
UNSTABLE_NAME_COLLISIONS,
120121
UNSTABLE_SYNTAX_PRE_EXPANSION,
@@ -4902,3 +4903,45 @@ declare_lint! {
49024903
reference:"issue #123743 <https://github.com/rust-lang/rust/issues/123743>",
49034904
};
49044905
}
4906+
4907+
declare_lint!{
4908+
/// The `unsafe_attr_outside_unsafe` lint detects a missing unsafe keyword
4909+
/// on attributes considered unsafe.
4910+
///
4911+
/// ### Example
4912+
///
4913+
/// ```rust
4914+
/// #![feature(unsafe_attributes)]
4915+
/// #![warn(unsafe_attr_outside_unsafe)]
4916+
///
4917+
/// #[no_mangle]
4918+
/// extern "C" fn foo() {}
4919+
///
4920+
/// fn main() {}
4921+
/// ```
4922+
///
4923+
/// {{produces}}
4924+
///
4925+
/// ### Explanation
4926+
///
4927+
/// Some attributes (e.g. `no_mangle`, `export_name`, `link_section` -- see
4928+
/// [issue #82499] for a more complete list) are considered "unsafe" attributes.
4929+
/// An unsafe attribute must only be used inside unsafe(...).
4930+
///
4931+
/// This lint can automatically wrap the attributes in `unsafe(...)` , but this
4932+
/// obviously cannot verify that the preconditions of the `unsafe`
4933+
/// attributes are fulfilled, so that is still up to the user.
4934+
///
4935+
/// The lint is currently "allow" by default, but that might change in the
4936+
/// future.
4937+
///
4938+
/// [editions]: https://doc.rust-lang.org/edition-guide/
4939+
/// [issue #82499]: https://github.com/rust-lang/rust/issues/82499
4940+
pubUNSAFE_ATTR_OUTSIDE_UNSAFE,
4941+
Allow,
4942+
"detects unsafe attributes outside of unsafe",
4943+
@future_incompatible = FutureIncompatibleInfo{
4944+
reason:FutureIncompatibilityReason::EditionError(Edition::Edition2024),
4945+
reference:"issue #123757 <https://github.com/rust-lang/rust/issues/123757>",
4946+
};
4947+
}

0 commit comments

Comments
 (0)