Skip to content

Commit 60694c7

Browse files
committed
tests
1 parent 7e62953 commit 60694c7

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

+738
-89
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,8 @@ pub enum DistributedSlice {
36113611
/// This const (we never do this to statics) represents an addition to a global registry
36123612
/// declared somewhere else.
36133613
Addition { declaration: Path, id: NodeId },
3614+
/// Applied to an invalid item, error guaranteed to have be emitted
3615+
Err(ErrorGuaranteed),
36143616
}
36153617

36163618
#[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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,6 @@ ast_lowering_yield = yield syntax is experimental
190190
ast_lowering_yield_in_closure =
191191
`yield` can only be used in `#[coroutine]` closures, or `gen` blocks
192192
.suggestion = use `#[coroutine]` to make this closure a coroutine
193+
194+
ast_lowering_distributed_slice_with_initializer =
195+
distributed slice elements are added with `distributed_slice_element!(...)`

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: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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::{self as hir, DistributedSlice, HirId, InvalidDistributedSliceDeclaration, LifetimeSource, PredicateOrigin};
99
use rustc_index::{IndexSlice, IndexVec};
1010
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1111
use rustc_span::edit_distance::find_best_match_for_name;
@@ -14,6 +14,8 @@ use smallvec::{SmallVec, smallvec};
1414
use thin_vec::ThinVec;
1515
use tracing::instrument;
1616

17+
use crate::errors::DistributedSliceWithInitializer;
18+
1719
use super::errors::{
1820
InvalidAbi, InvalidAbiSuggestion, MisplacedRelaxTraitBound, TupleStructWithDefault,
1921
};
@@ -154,6 +156,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
154156
) -> DistributedSlice {
155157
match distributed_slice {
156158
ast::DistributedSlice::None => DistributedSlice::None,
159+
ast::DistributedSlice::Err(_) => DistributedSlice::None,
157160
ast::DistributedSlice::Declaration(span, _) => {
158161
DistributedSlice::Declaration(self.lower_span(*span))
159162
}
@@ -733,13 +736,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
733736
let ty = self.lower_ty(
734737
ty,
735738
ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy),
736-
false,
739+
matches!(distributed_slice, ast::DistributedSlice::Err(_)),
737740
);
738741
let safety = self.lower_safety(*safety, hir::Safety::Unsafe);
739742
if define_opaque.is_some() {
740743
self.dcx().span_err(i.span, "foreign statics cannot define opaque types");
741744
}
742-
(ident, hir::ForeignItemKind::Static(ty, *mutability, safety))
745+
(ident, hir::ForeignItemKind::Static(ty, *mutability, safety, if let ast::DistributedSlice::Err(eg) = distributed_slice {
746+
InvalidDistributedSliceDeclaration::Yes(*eg)
747+
} else {
748+
InvalidDistributedSliceDeclaration::No
749+
}))
743750
}
744751
ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => {
745752
(ident, hir::ForeignItemKind::Type)
@@ -861,6 +868,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
861868
ty,
862869
expr,
863870
define_opaque,
871+
distributed_slice,
864872
..
865873
}) => {
866874
let (generics, kind) = self.lower_generics(
@@ -871,12 +879,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
871879
let ty = this.lower_ty(
872880
ty,
873881
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
874-
false,
882+
matches!(distributed_slice, ast::DistributedSlice::Err(_)),
875883
);
876884
let body =
877885
expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x), None));
878886

879-
hir::TraitItemKind::Const(ty, body)
887+
hir::TraitItemKind::Const(ty, body, if let ast::DistributedSlice::Err(eg) = distributed_slice {
888+
InvalidDistributedSliceDeclaration::Yes(*eg)
889+
} else {
890+
InvalidDistributedSliceDeclaration::No
891+
})
880892
},
881893
);
882894

@@ -1058,6 +1070,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10581070
ty,
10591071
expr,
10601072
define_opaque,
1073+
distributed_slice,
10611074
..
10621075
}) => (
10631076
*ident,
@@ -1069,11 +1082,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
10691082
let ty = this.lower_ty(
10701083
ty,
10711084
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
1072-
false,
1085+
matches!(distributed_slice, ast::DistributedSlice::Err(_)),
10731086
);
10741087
let body = this.lower_const_body(i.span, expr.as_deref(), None);
10751088
this.lower_define_opaque(hir_id, &define_opaque);
1076-
hir::ImplItemKind::Const(ty, body)
1089+
hir::ImplItemKind::Const(ty, body, if let ast::DistributedSlice::Err(eg) = distributed_slice {
1090+
InvalidDistributedSliceDeclaration::Yes(*eg)
1091+
} else {
1092+
InvalidDistributedSliceDeclaration::No
1093+
})
10771094
},
10781095
),
10791096
),
@@ -1369,7 +1386,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
13691386
span: this.lower_span(span),
13701387
}
13711388
}
1372-
(Some(expr), Some(_)) => panic!("distributed slice with initializer"),
1389+
(Some(_), Some(node_id)) => {
1390+
let eg = this.tcx.dcx().emit_err(DistributedSliceWithInitializer {
1391+
span,
1392+
});
1393+
1394+
let expr_hir_id = this.lower_node_id(node_id);
1395+
hir::Expr {
1396+
hir_id: expr_hir_id,
1397+
kind: rustc_hir::ExprKind::Err(eg),
1398+
span: this.lower_span(span),
1399+
}
1400+
},
13731401
(None, None) => {
13741402
this.expr_err(span, this.dcx().span_delayed_bug(span, "no block"))
13751403
}

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,24 @@ builtin_macros_unexpected_lit = expected path to a trait, found literal
284284
.label = not a trait
285285
.str_lit = try using `#[derive({$sym})]`
286286
.other = for example, write `#[derive(Debug)]` for `Debug`
287+
288+
builtin_macros_distributed_slice_expected_const_static =
289+
expected this to be a const or a static
290+
.label = because of this attribute
291+
292+
builtin_macros_distributed_slice_foreign_item =
293+
expected this to be a non-extern const or a static
294+
.note = this is inside an `extern` block
295+
.label = because of this attribute
296+
297+
builtin_macros_distributed_slice_assoc_item =
298+
expected this to be a module-level const or a static
299+
.note = this is an associated item
300+
.label = because of this attribute
301+
302+
builtin_macros_distributed_slice_expected_crate =
303+
`#[distributed_slice]` must take one parameter `crate`
304+
.suggestion = add `crate`
305+
306+
builtin_macros_distributed_slice_generic =
307+
distributed slices can't be generic

compiler/rustc_builtin_macros/src/distributed_slice.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
ast, AssocItemKind, ConstItem, Defaultness, DistributedSlice, Expr, ForeignItemKind, Generics, Item, ItemKind, Path, Ty, TyKind, DUMMY_NODE_ID
65
};
76
use rustc_errors::PResult;
87
use rustc_expand::base::{
@@ -14,34 +13,64 @@ use rustc_span::{Ident, Span, kw};
1413
use smallvec::smallvec;
1514
use thin_vec::ThinVec;
1615

16+
use crate::errors::{DistributedSliceAssocItem, DistributedSliceExpectedConstStatic, DistributedSliceExpectedCrate, DistributedSliceForeignItem, DistributedSliceGeneric};
17+
1718
/// ```rust
1819
/// #[distributed_slice(crate)]
1920
/// const MEOWS: [&str; _];
2021
/// ```
2122
pub(crate) fn distributed_slice(
22-
_ecx: &mut ExtCtxt<'_>,
23+
ecx: &mut ExtCtxt<'_>,
2324
span: Span,
24-
_meta_item: &ast::MetaItem,
25+
meta_item: &ast::MetaItem,
2526
mut orig_item: Annotatable,
2627
) -> Vec<Annotatable> {
2728
// TODO: FIXME(gr)
28-
// FIXME(gr): check item
29+
30+
if let Some([ast::MetaItemInner::MetaItem(mi)]) = meta_item.meta_item_list() {
31+
if !mi.is_word() || !mi.path.is_ident(kw::Crate) {
32+
ecx.dcx().emit_err(DistributedSliceExpectedCrate { span: meta_item.span });
33+
}
34+
} else {
35+
ecx.dcx().emit_err(DistributedSliceExpectedCrate { span: meta_item.span });
36+
};
37+
38+
let item_span = orig_item.span();
2939

3040
let Annotatable::Item(item) = &mut orig_item else {
31-
panic!("expected `#[distributed_slice(crate)]` on an item")
41+
if let Annotatable::ForeignItem(fi) = &mut orig_item {
42+
let eg = ecx.dcx().emit_err(DistributedSliceForeignItem { span: item_span, attr_span: meta_item.span });
43+
44+
if let ForeignItemKind::Static(static_item) = &mut fi.kind {
45+
static_item.distributed_slice = DistributedSlice::Err(eg);
46+
}
47+
} else if let Annotatable::AssocItem(ai, ..) = &mut orig_item {
48+
let eg = ecx.dcx().emit_err(DistributedSliceAssocItem { span: item_span, attr_span: meta_item.span });
49+
50+
if let AssocItemKind::Const(const_item) = &mut ai.kind {
51+
const_item.distributed_slice = DistributedSlice::Err(eg);
52+
}
53+
} else {
54+
ecx.dcx().emit_err(DistributedSliceExpectedConstStatic { span:orig_item.span(), attr_span: meta_item.span });
55+
}
56+
57+
return vec![orig_item]
3258
};
3359

3460
match &mut item.kind {
3561
ItemKind::Static(static_item) => {
3662
static_item.distributed_slice = DistributedSlice::Declaration(span, DUMMY_NODE_ID);
3763
}
3864
ItemKind::Const(const_item) => {
65+
if !const_item.generics.params.is_empty() || !const_item.generics.where_clause.is_empty() {
66+
ecx.dcx().emit_err(DistributedSliceGeneric { span: item_span });
67+
}
68+
3969
const_item.distributed_slice = DistributedSlice::Declaration(span, DUMMY_NODE_ID);
4070
}
41-
other => {
42-
panic!(
43-
"expected `#[distributed_slice(crate)]` on a const or static item, not {other:?}"
44-
);
71+
_ => {
72+
ecx.dcx().emit_err(DistributedSliceExpectedConstStatic { span: item.span, attr_span: meta_item.span });
73+
return vec![orig_item]
4574
}
4675
}
4776

@@ -69,7 +98,10 @@ pub(crate) fn distributed_slice_element(
6998
) -> MacroExpanderResult<'static> {
7099
let (path, expr) = match parse_element(cx.new_parser_from_tts(tts)) {
71100
Ok((ident, expr)) => (ident, expr),
72-
Err(err) => {
101+
Err(mut err) => {
102+
if err.span.is_dummy() {
103+
err.span(span);
104+
}
73105
let guar = err.emit();
74106
return ExpandResult::Ready(DummyResult::any(span, guar));
75107
}

compiler/rustc_builtin_macros/src/errors.rs

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

0 commit comments

Comments
 (0)