Skip to content

Commit 0314c6f

Browse files
committed
tests
1 parent 9f5725c commit 0314c6f

Some content is hidden

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

55 files changed

+798
-89
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,6 +3624,8 @@ pub enum DistributedSlice {
36243624
/// This const (we never do this to statics) represents an addition to a global registry
36253625
/// declared somewhere else.
36263626
Addition { declaration: Path, id: NodeId },
3627+
/// Applied to an invalid item, error guaranteed to have be emitted
3628+
Err(ErrorGuaranteed),
36273629
}
36283630

36293631
#[derive(Clone, Encodable, Decodable, Debug)]

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ macro_rules! common_visitor_and_walkers {
458458
visit_opt!(vis, visit_expr, expr);
459459
match distributed_slice {
460460
DistributedSlice::None => {}
461+
DistributedSlice::Err(..) => {}
461462
DistributedSlice::Declaration(span, id) => {
462463
try_visit!(visit_span(vis, span));
463464
try_visit!(visit_id(vis, id));
@@ -635,6 +636,7 @@ macro_rules! common_visitor_and_walkers {
635636

636637
match distributed_slice {
637638
DistributedSlice::None => {}
639+
DistributedSlice::Err(..) => {}
638640
DistributedSlice::Declaration(span, id) => {
639641
try_visit!(visit_span(vis, span));
640642
try_visit!(visit_id(vis, id));

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ ast_lowering_coroutine_too_many_parameters =
5656
ast_lowering_default_field_in_tuple = default fields are not supported in tuple structs
5757
.label = default fields are only supported on structs
5858
59+
ast_lowering_distributed_slice_with_initializer =
60+
distributed slice elements are added with `distributed_slice_element!(...)`
5961
ast_lowering_does_not_support_modifiers =
6062
the `{$class_name}` register class does not support template modifiers
6163

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,10 @@ pub(crate) struct UseConstGenericArg {
475475
#[suggestion_part(code = "{other_args}")]
476476
pub call_args: Span,
477477
}
478+
479+
#[derive(Diagnostic)]
480+
#[diag(ast_lowering_distributed_slice_with_initializer)]
481+
pub(crate) struct DistributedSliceWithInitializer {
482+
#[primary_span]
483+
pub span: Span,
484+
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use rustc_ast::*;
55
use rustc_errors::ErrorGuaranteed;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
8-
use rustc_hir::{self as hir, DistributedSlice, HirId, LifetimeSource, PredicateOrigin};
8+
use rustc_hir::{
9+
self as hir, DistributedSlice, HirId, InvalidDistributedSliceDeclaration, LifetimeSource,
10+
PredicateOrigin,
11+
};
912
use rustc_index::{IndexSlice, IndexVec};
1013
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1114
use rustc_span::edit_distance::find_best_match_for_name;
@@ -22,6 +25,7 @@ use super::{
2225
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2326
ResolverAstLoweringExt,
2427
};
28+
use crate::errors::DistributedSliceWithInitializer;
2529

2630
pub(super) struct ItemLowerer<'a, 'hir> {
2731
pub(super) tcx: TyCtxt<'hir>,
@@ -154,6 +158,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
154158
) -> DistributedSlice {
155159
match distributed_slice {
156160
ast::DistributedSlice::None => DistributedSlice::None,
161+
ast::DistributedSlice::Err(_) => DistributedSlice::None,
157162
ast::DistributedSlice::Declaration(span, _) => {
158163
DistributedSlice::Declaration(self.lower_span(*span))
159164
}
@@ -733,13 +738,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
733738
let ty = self.lower_ty(
734739
ty,
735740
ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy),
736-
false,
741+
matches!(distributed_slice, ast::DistributedSlice::Err(_)),
737742
);
738743
let safety = self.lower_safety(*safety, hir::Safety::Unsafe);
739744
if define_opaque.is_some() {
740745
self.dcx().span_err(i.span, "foreign statics cannot define opaque types");
741746
}
742-
(ident, hir::ForeignItemKind::Static(ty, *mutability, safety))
747+
(
748+
ident,
749+
hir::ForeignItemKind::Static(
750+
ty,
751+
*mutability,
752+
safety,
753+
if let ast::DistributedSlice::Err(eg) = distributed_slice {
754+
InvalidDistributedSliceDeclaration::Yes(*eg)
755+
} else {
756+
InvalidDistributedSliceDeclaration::No
757+
},
758+
),
759+
)
743760
}
744761
ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => {
745762
(ident, hir::ForeignItemKind::Type)
@@ -861,6 +878,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
861878
ty,
862879
expr,
863880
define_opaque,
881+
distributed_slice,
864882
..
865883
}) => {
866884
let (generics, kind) = self.lower_generics(
@@ -871,12 +889,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
871889
let ty = this.lower_ty(
872890
ty,
873891
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
874-
false,
892+
matches!(distributed_slice, ast::DistributedSlice::Err(_)),
875893
);
876894
let body =
877895
expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x), None));
878896

879-
hir::TraitItemKind::Const(ty, body)
897+
hir::TraitItemKind::Const(
898+
ty,
899+
body,
900+
if let ast::DistributedSlice::Err(eg) = distributed_slice {
901+
InvalidDistributedSliceDeclaration::Yes(*eg)
902+
} else {
903+
InvalidDistributedSliceDeclaration::No
904+
},
905+
)
880906
},
881907
);
882908

@@ -1058,6 +1084,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10581084
ty,
10591085
expr,
10601086
define_opaque,
1087+
distributed_slice,
10611088
..
10621089
}) => (
10631090
*ident,
@@ -1069,11 +1096,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
10691096
let ty = this.lower_ty(
10701097
ty,
10711098
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
1072-
false,
1099+
matches!(distributed_slice, ast::DistributedSlice::Err(_)),
10731100
);
10741101
let body = this.lower_const_body(i.span, expr.as_deref(), None);
10751102
this.lower_define_opaque(hir_id, &define_opaque);
1076-
hir::ImplItemKind::Const(ty, body)
1103+
hir::ImplItemKind::Const(
1104+
ty,
1105+
body,
1106+
if let ast::DistributedSlice::Err(eg) = distributed_slice {
1107+
InvalidDistributedSliceDeclaration::Yes(*eg)
1108+
} else {
1109+
InvalidDistributedSliceDeclaration::No
1110+
},
1111+
)
10771112
},
10781113
),
10791114
),
@@ -1369,7 +1404,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
13691404
span: this.lower_span(span),
13701405
}
13711406
}
1372-
(Some(expr), Some(_)) => panic!("distributed slice with initializer"),
1407+
(Some(_), Some(node_id)) => {
1408+
let eg = this.tcx.dcx().emit_err(DistributedSliceWithInitializer { span });
1409+
1410+
let expr_hir_id = this.lower_node_id(node_id);
1411+
hir::Expr {
1412+
hir_id: expr_hir_id,
1413+
kind: rustc_hir::ExprKind::Err(eg),
1414+
span: this.lower_span(span),
1415+
}
1416+
}
13731417
(None, None) => {
13741418
this.expr_err(span, this.dcx().span_delayed_bug(span, "no block"))
13751419
}

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,11 +1443,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14431443

14441444
if let AssocCtxt::Impl { .. } = ctxt {
14451445
match &item.kind {
1446-
AssocItemKind::Const(box ConstItem { expr: None, .. }) => {
1447-
self.dcx().emit_err(errors::AssocConstWithoutBody {
1448-
span: item.span,
1449-
replace_span: self.ending_semi_or_hi(item.span),
1450-
});
1446+
AssocItemKind::Const(box ConstItem { expr: None, distributed_slice, .. }) => {
1447+
if !matches!(distributed_slice, DistributedSlice::Err(..)) {
1448+
self.dcx().emit_err(errors::AssocConstWithoutBody {
1449+
span: item.span,
1450+
replace_span: self.ending_semi_or_hi(item.span),
1451+
});
1452+
}
14511453
}
14521454
AssocItemKind::Fn(box Fn { body, .. }) => {
14531455
if body.is_none() && !self.is_sdylib_interface {

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ impl<'a> State<'a> {
222222
define_opaque,
223223
distributed_slice: _,
224224
}) => {
225-
// FIXME(gr): pretty print global registry
226225
self.print_item_const(
227226
*ident,
228227
None,

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,26 @@ builtin_macros_derive_path_args_list = traits in `#[derive(...)]` don't accept a
134134
builtin_macros_derive_path_args_value = traits in `#[derive(...)]` don't accept values
135135
.suggestion = remove the value
136136
137+
builtin_macros_distributed_slice_assoc_item =
138+
expected this to be a module-level const or a static
139+
.note = this is an associated item
140+
.label = because of this attribute
141+
142+
builtin_macros_distributed_slice_expected_const_static =
143+
expected this to be a const or a static
144+
.label = because of this attribute
145+
146+
builtin_macros_distributed_slice_expected_crate =
147+
`#[distributed_slice]` must take one parameter `crate`
148+
.suggestion = add `crate`
149+
150+
builtin_macros_distributed_slice_foreign_item =
151+
expected this to be a non-extern const or a static
152+
.note = this is inside an `extern` block
153+
.label = because of this attribute
154+
155+
builtin_macros_distributed_slice_generic =
156+
distributed slices can't be generic
137157
builtin_macros_env_not_defined = environment variable `{$var}` not defined at compile time
138158
.cargo = Cargo sets build script variables at run time. Use `std::env::var({$var_expr})` instead
139159
.custom = use `std::env::var({$var_expr})` to read the variable at run time

compiler/rustc_builtin_macros/src/distributed_slice.rs

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_ast::ptr::P;
22
use rustc_ast::tokenstream::TokenStream;
33
use rustc_ast::{
4-
ConstItem, DUMMY_NODE_ID, Defaultness, DistributedSlice, Expr, Generics, Item, ItemKind, Path,
5-
Ty, TyKind, ast,
4+
AssocItemKind, ConstItem, DUMMY_NODE_ID, Defaultness, DistributedSlice, Expr, ForeignItemKind,
5+
Generics, Item, ItemKind, Path, Ty, TyKind, ast,
66
};
77
use rustc_errors::PResult;
88
use rustc_expand::base::{
@@ -14,34 +14,80 @@ use rustc_span::{Ident, Span, kw};
1414
use smallvec::smallvec;
1515
use thin_vec::ThinVec;
1616

17+
use crate::errors::{
18+
DistributedSliceAssocItem, DistributedSliceExpectedConstStatic, DistributedSliceExpectedCrate,
19+
DistributedSliceForeignItem, DistributedSliceGeneric,
20+
};
21+
1722
/// ```rust
1823
/// #[distributed_slice(crate)]
1924
/// const MEOWS: [&str; _];
2025
/// ```
2126
pub(crate) fn distributed_slice(
22-
_ecx: &mut ExtCtxt<'_>,
27+
ecx: &mut ExtCtxt<'_>,
2328
span: Span,
24-
_meta_item: &ast::MetaItem,
29+
meta_item: &ast::MetaItem,
2530
mut orig_item: Annotatable,
2631
) -> Vec<Annotatable> {
2732
// TODO: FIXME(gr)
28-
// FIXME(gr): check item
33+
34+
if let Some([ast::MetaItemInner::MetaItem(mi)]) = meta_item.meta_item_list() {
35+
if !mi.is_word() || !mi.path.is_ident(kw::Crate) {
36+
ecx.dcx().emit_err(DistributedSliceExpectedCrate { span: meta_item.span });
37+
}
38+
} else {
39+
ecx.dcx().emit_err(DistributedSliceExpectedCrate { span: meta_item.span });
40+
};
41+
42+
let item_span = orig_item.span();
2943

3044
let Annotatable::Item(item) = &mut orig_item else {
31-
panic!("expected `#[distributed_slice(crate)]` on an item")
45+
if let Annotatable::ForeignItem(fi) = &mut orig_item {
46+
let eg = ecx.dcx().emit_err(DistributedSliceForeignItem {
47+
span: item_span,
48+
attr_span: meta_item.span,
49+
});
50+
51+
if let ForeignItemKind::Static(static_item) = &mut fi.kind {
52+
static_item.distributed_slice = DistributedSlice::Err(eg);
53+
}
54+
} else if let Annotatable::AssocItem(ai, ..) = &mut orig_item {
55+
let eg = ecx
56+
.dcx()
57+
.emit_err(DistributedSliceAssocItem { span: item_span, attr_span: meta_item.span });
58+
59+
if let AssocItemKind::Const(const_item) = &mut ai.kind {
60+
const_item.distributed_slice = DistributedSlice::Err(eg);
61+
}
62+
} else {
63+
ecx.dcx().emit_err(DistributedSliceExpectedConstStatic {
64+
span: orig_item.span(),
65+
attr_span: meta_item.span,
66+
});
67+
}
68+
69+
return vec![orig_item];
3270
};
3371

3472
match &mut item.kind {
3573
ItemKind::Static(static_item) => {
3674
static_item.distributed_slice = DistributedSlice::Declaration(span, DUMMY_NODE_ID);
3775
}
3876
ItemKind::Const(const_item) => {
77+
if !const_item.generics.params.is_empty()
78+
|| !const_item.generics.where_clause.is_empty()
79+
{
80+
ecx.dcx().emit_err(DistributedSliceGeneric { span: item_span });
81+
}
82+
3983
const_item.distributed_slice = DistributedSlice::Declaration(span, DUMMY_NODE_ID);
4084
}
41-
other => {
42-
panic!(
43-
"expected `#[distributed_slice(crate)]` on a const or static item, not {other:?}"
44-
);
85+
_ => {
86+
ecx.dcx().emit_err(DistributedSliceExpectedConstStatic {
87+
span: item.span,
88+
attr_span: meta_item.span,
89+
});
90+
return vec![orig_item];
4591
}
4692
}
4793

@@ -69,7 +115,10 @@ pub(crate) fn distributed_slice_element(
69115
) -> MacroExpanderResult<'static> {
70116
let (path, expr) = match parse_element(cx.new_parser_from_tts(tts)) {
71117
Ok((ident, expr)) => (ident, expr),
72-
Err(err) => {
118+
Err(mut err) => {
119+
if err.span.is_dummy() {
120+
err.span(span);
121+
}
73122
let guar = err.emit();
74123
return ExpandResult::Ready(DummyResult::any(span, guar));
75124
}

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,3 +965,51 @@ pub(crate) struct AsmExpectedOther {
965965
pub(crate) span: Span,
966966
pub(crate) is_inline_asm: bool,
967967
}
968+
969+
#[derive(Diagnostic)]
970+
#[diag(builtin_macros_distributed_slice_expected_const_static)]
971+
pub(crate) struct DistributedSliceExpectedConstStatic {
972+
#[primary_span]
973+
pub(crate) span: Span,
974+
#[label]
975+
pub(crate) attr_span: Span,
976+
}
977+
978+
#[derive(Diagnostic)]
979+
#[diag(builtin_macros_distributed_slice_foreign_item)]
980+
#[note]
981+
pub(crate) struct DistributedSliceForeignItem {
982+
#[primary_span]
983+
pub(crate) span: Span,
984+
#[label]
985+
pub(crate) attr_span: Span,
986+
}
987+
988+
#[derive(Diagnostic)]
989+
#[diag(builtin_macros_distributed_slice_assoc_item)]
990+
#[note]
991+
pub(crate) struct DistributedSliceAssocItem {
992+
#[primary_span]
993+
pub(crate) span: Span,
994+
#[label]
995+
pub(crate) attr_span: Span,
996+
}
997+
998+
#[derive(Diagnostic)]
999+
#[diag(builtin_macros_distributed_slice_expected_crate)]
1000+
pub(crate) struct DistributedSliceExpectedCrate {
1001+
#[primary_span]
1002+
#[suggestion(
1003+
code = "#[distributed_slice(crate)]",
1004+
style = "verbose",
1005+
applicability = "maybe-incorrect"
1006+
)]
1007+
pub(crate) span: Span,
1008+
}
1009+
1010+
#[derive(Diagnostic)]
1011+
#[diag(builtin_macros_distributed_slice_generic)]
1012+
pub(crate) struct DistributedSliceGeneric {
1013+
#[primary_span]
1014+
pub(crate) span: Span,
1015+
}

0 commit comments

Comments
 (0)