Skip to content

Commit 4324d85

Browse files
authored
Merge branch 'rust-lang:master' into master
2 parents c6e9d82 + 5a3e2a4 commit 4324d85

File tree

1,699 files changed

+27230
-15705
lines changed

Some content is hidden

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

1,699 files changed

+27230
-15705
lines changed

Cargo.lock

Lines changed: 86 additions & 175 deletions
Large diffs are not rendered by default.

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,38 +3184,6 @@ pub struct StaticItem {
31843184
pub expr: Option<P<Expr>>,
31853185
}
31863186

3187-
/// A static item in `extern` block.
3188-
// This struct is identical to StaticItem for now but it's going to have a safety attribute.
3189-
#[derive(Clone, Encodable, Decodable, Debug)]
3190-
pub struct StaticForeignItem {
3191-
pub ty: P<Ty>,
3192-
pub safety: Safety,
3193-
pub mutability: Mutability,
3194-
pub expr: Option<P<Expr>>,
3195-
}
3196-
3197-
impl From<StaticItem> for StaticForeignItem {
3198-
fn from(static_item: StaticItem) -> StaticForeignItem {
3199-
StaticForeignItem {
3200-
ty: static_item.ty,
3201-
safety: static_item.safety,
3202-
mutability: static_item.mutability,
3203-
expr: static_item.expr,
3204-
}
3205-
}
3206-
}
3207-
3208-
impl From<StaticForeignItem> for StaticItem {
3209-
fn from(static_item: StaticForeignItem) -> StaticItem {
3210-
StaticItem {
3211-
ty: static_item.ty,
3212-
safety: static_item.safety,
3213-
mutability: static_item.mutability,
3214-
expr: static_item.expr,
3215-
}
3216-
}
3217-
}
3218-
32193187
#[derive(Clone, Encodable, Decodable, Debug)]
32203188
pub struct ConstItem {
32213189
pub defaultness: Defaultness,
@@ -3430,7 +3398,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
34303398
#[derive(Clone, Encodable, Decodable, Debug)]
34313399
pub enum ForeignItemKind {
34323400
/// A foreign static item (`static FOO: u8`).
3433-
Static(Box<StaticForeignItem>),
3401+
Static(Box<StaticItem>),
34343402
/// An foreign function.
34353403
Fn(Box<Fn>),
34363404
/// An foreign type.

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,12 +1310,7 @@ pub fn noop_flat_map_item<K: NoopVisitItemKind>(
13101310
impl NoopVisitItemKind for ForeignItemKind {
13111311
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
13121312
match self {
1313-
ForeignItemKind::Static(box StaticForeignItem {
1314-
ty,
1315-
mutability: _,
1316-
expr,
1317-
safety: _,
1318-
}) => {
1313+
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
13191314
visitor.visit_ty(ty);
13201315
visit_opt(expr, |expr| visitor.visit_expr(expr));
13211316
}

compiler/rustc_ast/src/token.rs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub use BinOpToken::*;
22
pub use LitKind::*;
33
pub use Nonterminal::*;
4+
pub use NtExprKind::*;
5+
pub use NtPatKind::*;
46
pub use TokenKind::*;
57

68
use crate::ast;
@@ -871,6 +873,27 @@ impl PartialEq<TokenKind> for Token {
871873
}
872874
}
873875

876+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable)]
877+
pub enum NtPatKind {
878+
// Matches or-patterns. Was written using `pat` in edition 2021 or later.
879+
PatWithOr,
880+
// Doesn't match or-patterns.
881+
// - `inferred`: was written using `pat` in edition 2015 or 2018.
882+
// - `!inferred`: was written using `pat_param`.
883+
PatParam { inferred: bool },
884+
}
885+
886+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable)]
887+
pub enum NtExprKind {
888+
// Matches expressions using the post-edition 2024. Was written using
889+
// `expr` in edition 2024 or later.
890+
Expr,
891+
// Matches expressions using the pre-edition 2024 rules.
892+
// - `inferred`: was written using `expr` in edition 2021 or earlier.
893+
// - `!inferred`: was written using `expr_2021`.
894+
Expr2021 { inferred: bool },
895+
}
896+
874897
#[derive(Clone, Encodable, Decodable)]
875898
/// For interpolation during macro expansion.
876899
pub enum Nonterminal {
@@ -892,15 +915,8 @@ pub enum NonterminalKind {
892915
Item,
893916
Block,
894917
Stmt,
895-
PatParam {
896-
/// Keep track of whether the user used `:pat_param` or `:pat` and we inferred it from the
897-
/// edition of the span. This is used for diagnostics.
898-
inferred: bool,
899-
},
900-
PatWithOr,
901-
Expr,
902-
/// Matches an expression using the rules from edition 2021 and earlier.
903-
Expr2021,
918+
Pat(NtPatKind),
919+
Expr(NtExprKind),
904920
Ty,
905921
Ident,
906922
Lifetime,
@@ -922,15 +938,22 @@ impl NonterminalKind {
922938
sym::item => NonterminalKind::Item,
923939
sym::block => NonterminalKind::Block,
924940
sym::stmt => NonterminalKind::Stmt,
925-
sym::pat => match edition() {
926-
Edition::Edition2015 | Edition::Edition2018 => {
927-
NonterminalKind::PatParam { inferred: true }
941+
sym::pat => {
942+
if edition().at_least_rust_2021() {
943+
NonterminalKind::Pat(PatWithOr)
944+
} else {
945+
NonterminalKind::Pat(PatParam { inferred: true })
928946
}
929-
Edition::Edition2021 | Edition::Edition2024 => NonterminalKind::PatWithOr,
930-
},
931-
sym::pat_param => NonterminalKind::PatParam { inferred: false },
932-
sym::expr => NonterminalKind::Expr,
933-
sym::expr_2021 if edition().at_least_rust_2021() => NonterminalKind::Expr2021,
947+
}
948+
sym::pat_param => NonterminalKind::Pat(PatParam { inferred: false }),
949+
sym::expr => {
950+
if edition().at_least_rust_2024() {
951+
NonterminalKind::Expr(Expr)
952+
} else {
953+
NonterminalKind::Expr(Expr2021 { inferred: true })
954+
}
955+
}
956+
sym::expr_2021 => NonterminalKind::Expr(Expr2021 { inferred: false }),
934957
sym::ty => NonterminalKind::Ty,
935958
sym::ident => NonterminalKind::Ident,
936959
sym::lifetime => NonterminalKind::Lifetime,
@@ -942,15 +965,16 @@ impl NonterminalKind {
942965
_ => return None,
943966
})
944967
}
968+
945969
fn symbol(self) -> Symbol {
946970
match self {
947971
NonterminalKind::Item => sym::item,
948972
NonterminalKind::Block => sym::block,
949973
NonterminalKind::Stmt => sym::stmt,
950-
NonterminalKind::PatParam { inferred: false } => sym::pat_param,
951-
NonterminalKind::PatParam { inferred: true } | NonterminalKind::PatWithOr => sym::pat,
952-
NonterminalKind::Expr => sym::expr,
953-
NonterminalKind::Expr2021 => sym::expr_2021,
974+
NonterminalKind::Pat(PatParam { inferred: true } | PatWithOr) => sym::pat,
975+
NonterminalKind::Pat(PatParam { inferred: false }) => sym::pat_param,
976+
NonterminalKind::Expr(Expr2021 { inferred: true } | Expr) => sym::expr,
977+
NonterminalKind::Expr(Expr2021 { inferred: false }) => sym::expr_2021,
954978
NonterminalKind::Ty => sym::ty,
955979
NonterminalKind::Ident => sym::ident,
956980
NonterminalKind::Lifetime => sym::lifetime,

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,12 +672,7 @@ impl WalkItemKind for ForeignItemKind {
672672
) -> V::Result {
673673
let &Item { id, span, ident, ref vis, .. } = item;
674674
match self {
675-
ForeignItemKind::Static(box StaticForeignItem {
676-
ty,
677-
mutability: _,
678-
expr,
679-
safety: _,
680-
}) => {
675+
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
681676
try_visit!(visitor.visit_ty(ty));
682677
visit_opt!(visitor, visit_expr, expr);
683678
}

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ast_lowering_inline_asm_unsupported_target =
7878
ast_lowering_invalid_abi =
7979
invalid ABI: found `{$abi}`
8080
.label = invalid ABI
81-
.note = invoke `{$command}` for a full list of supported calling conventions.
81+
.note = invoke `{$command}` for a full list of supported calling conventions
8282
8383
ast_lowering_invalid_abi_clobber_abi =
8484
invalid ABI for `clobber_abi`

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
664664

665665
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics, safety)
666666
}
667-
ForeignItemKind::Static(box StaticForeignItem {
668-
ty,
669-
mutability,
670-
expr: _,
671-
safety,
672-
}) => {
667+
ForeignItemKind::Static(box StaticItem { ty, mutability, expr: _, safety }) => {
673668
let ty = self
674669
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
675670
let safety = self.lower_safety(*safety, hir::Safety::Unsafe);

compiler/rustc_ast_passes/messages.ftl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ ast_passes_auto_super_lifetime = auto traits cannot have super traits or lifetim
2828
.label = {ast_passes_auto_super_lifetime}
2929
.suggestion = remove the super traits or lifetime bounds
3030
31-
ast_passes_bad_c_variadic = only foreign or `unsafe extern "C"` functions may be C-variadic
31+
ast_passes_bad_c_variadic = only foreign, `unsafe extern "C"`, or `unsafe extern "C-unwind"` functions may have a C-variadic arg
32+
33+
ast_passes_bare_fn_invalid_safety = function pointers cannot be declared with `safe` safety qualifier
34+
.suggestion = remove safe from this item
3235
3336
ast_passes_body_in_extern = incorrect `{$kind}` inside `extern` block
3437
.cannot_have = cannot have a body
@@ -167,6 +170,9 @@ ast_passes_invalid_unnamed_field_ty =
167170
unnamed fields can only have struct or union types
168171
.label = not a struct or union
169172
173+
ast_passes_item_invalid_safety = items outside of `unsafe extern {"{ }"}` cannot be declared with `safe` safety qualifier
174+
.suggestion = remove safe from this item
175+
170176
ast_passes_item_underscore = `{$kind}` items in this context need a name
171177
.label = `_` is not a valid name for this `{$kind}` item
172178

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,29 @@ impl<'a> AstValidator<'a> {
456456
}
457457
}
458458

459-
fn check_foreign_item_safety(&self, item_span: Span, safety: Safety) {
460-
if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_))
461-
&& (self.extern_mod_safety == Some(Safety::Default)
462-
|| !self.features.unsafe_extern_blocks)
463-
{
464-
self.dcx().emit_err(errors::InvalidSafetyOnExtern {
465-
item_span,
466-
block: self.current_extern_span(),
467-
});
459+
fn check_item_safety(&self, span: Span, safety: Safety) {
460+
match self.extern_mod_safety {
461+
Some(extern_safety) => {
462+
if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_))
463+
&& (extern_safety == Safety::Default || !self.features.unsafe_extern_blocks)
464+
{
465+
self.dcx().emit_err(errors::InvalidSafetyOnExtern {
466+
item_span: span,
467+
block: self.current_extern_span(),
468+
});
469+
}
470+
}
471+
None => {
472+
if matches!(safety, Safety::Safe(_)) {
473+
self.dcx().emit_err(errors::InvalidSafetyOnItem { span });
474+
}
475+
}
476+
}
477+
}
478+
479+
fn check_bare_fn_safety(&self, span: Span, safety: Safety) {
480+
if matches!(safety, Safety::Safe(_)) {
481+
self.dcx().emit_err(errors::InvalidSafetyOnBareFn { span });
468482
}
469483
}
470484

@@ -623,6 +637,7 @@ impl<'a> AstValidator<'a> {
623637
(Some(FnCtxt::Foreign), _) => return,
624638
(Some(FnCtxt::Free), Some(header)) => match header.ext {
625639
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }, _)
640+
| Extern::Explicit(StrLit { symbol_unescaped: sym::C_dash_unwind, .. }, _)
626641
| Extern::Implicit(_)
627642
if matches!(header.safety, Safety::Unsafe(_)) =>
628643
{
@@ -746,6 +761,7 @@ impl<'a> AstValidator<'a> {
746761
fn visit_ty_common(&mut self, ty: &'a Ty) {
747762
match &ty.kind {
748763
TyKind::BareFn(bfty) => {
764+
self.check_bare_fn_safety(bfty.decl_span, bfty.safety);
749765
self.check_fn_decl(&bfty.decl, SelfSemantic::No);
750766
Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
751767
self.dcx().emit_err(errors::PatternFnPointer { span });
@@ -883,7 +899,7 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara
883899

884900
impl<'a> Visitor<'a> for AstValidator<'a> {
885901
fn visit_attribute(&mut self, attr: &Attribute) {
886-
validate_attr::check_attr(&self.session.psess, attr);
902+
validate_attr::check_attr(&self.features, &self.session.psess, attr);
887903
}
888904

889905
fn visit_ty(&mut self, ty: &'a Ty) {
@@ -1174,11 +1190,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11741190
});
11751191
}
11761192
}
1177-
ItemKind::Static(box StaticItem { expr: None, .. }) => {
1178-
self.dcx().emit_err(errors::StaticWithoutBody {
1179-
span: item.span,
1180-
replace_span: self.ending_semi_or_hi(item.span),
1181-
});
1193+
ItemKind::Static(box StaticItem { expr, safety, .. }) => {
1194+
self.check_item_safety(item.span, *safety);
1195+
1196+
if expr.is_none() {
1197+
self.dcx().emit_err(errors::StaticWithoutBody {
1198+
span: item.span,
1199+
replace_span: self.ending_semi_or_hi(item.span),
1200+
});
1201+
}
11821202
}
11831203
ItemKind::TyAlias(
11841204
ty_alias @ box TyAlias { defaultness, bounds, where_clauses, ty, .. },
@@ -1212,7 +1232,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12121232
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
12131233
match &fi.kind {
12141234
ForeignItemKind::Fn(box Fn { defaultness, sig, body, .. }) => {
1215-
self.check_foreign_item_safety(fi.span, sig.header.safety);
12161235
self.check_defaultness(fi.span, *defaultness);
12171236
self.check_foreign_fn_bodyless(fi.ident, body.as_deref());
12181237
self.check_foreign_fn_headerless(sig.header);
@@ -1232,8 +1251,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12321251
self.check_foreign_ty_genericless(generics, where_clauses);
12331252
self.check_foreign_item_ascii_only(fi.ident);
12341253
}
1235-
ForeignItemKind::Static(box StaticForeignItem { expr, safety, .. }) => {
1236-
self.check_foreign_item_safety(fi.span, *safety);
1254+
ForeignItemKind::Static(box StaticItem { expr, safety, .. }) => {
1255+
self.check_item_safety(fi.span, *safety);
12371256
self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|b| b.span));
12381257
self.check_foreign_item_ascii_only(fi.ident);
12391258
}
@@ -1453,6 +1472,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14531472
};
14541473
self.check_fn_decl(fk.decl(), self_semantic);
14551474

1475+
if let Some(&FnHeader { safety, .. }) = fk.header() {
1476+
self.check_item_safety(span, safety);
1477+
}
1478+
14561479
self.check_c_variadic_type(fk);
14571480

14581481
// Functions cannot both be `const async` or `const gen`

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ pub struct InvalidSafetyOnExtern {
225225
pub block: Span,
226226
}
227227

228+
#[derive(Diagnostic)]
229+
#[diag(ast_passes_item_invalid_safety)]
230+
pub struct InvalidSafetyOnItem {
231+
#[primary_span]
232+
pub span: Span,
233+
}
234+
235+
#[derive(Diagnostic)]
236+
#[diag(ast_passes_bare_fn_invalid_safety)]
237+
pub struct InvalidSafetyOnBareFn {
238+
#[primary_span]
239+
pub span: Span,
240+
}
241+
228242
#[derive(Diagnostic)]
229243
#[diag(ast_passes_bound_in_context)]
230244
pub struct BoundInContext<'a> {

0 commit comments

Comments
 (0)