Skip to content

Commit c4aa863

Browse files
committed
Auto merge of #153180 - JonathanBrouwer:rollup-5vxerGq, r=JonathanBrouwer
Rollup of 11 pull requests Successful merges: - #151431 (Add new unstable attribute: `#[export_visibility = ...]`.) - #153012 (Stop using `LinkedGraph` in `lexical_region_resolve`) - #153179 (Force a CI LLVM stamp bump) - #150828 (Improved security section in rustdoc for `current_exe`) - #152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate) - #152674 (rustc_public: remove the `CrateDefItems` trait) - #153073 (Fix mem::conjure_zst panic message to use any::type_name instead) - #153117 (Remove mutation from macro path URL construction) - #153128 (Recover feature lang_items for emscripten) - #153138 (Print path root when printing path) - #153159 (Work around a false `err.emit();` type error in rust-analyzer)
2 parents 6f54d59 + 9fc4191 commit c4aa863

48 files changed

Lines changed: 693 additions & 194 deletions

File tree

Some content is hidden

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

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use rustc_hir::attrs::{CoverageAttrKind, OptimizeAttr, RtsanSetting, SanitizerSet, UsedBy};
1+
use rustc_hir::attrs::{
2+
CoverageAttrKind, ExportVisibilityAttrValue, OptimizeAttr, RtsanSetting, SanitizerSet, UsedBy,
3+
};
24
use rustc_session::parse::feature_err;
35

46
use super::prelude::*;
@@ -153,6 +155,43 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
153155
}
154156
}
155157

158+
pub(crate) struct ExportVisibilityParser;
159+
160+
impl<S: Stage> SingleAttributeParser<S> for ExportVisibilityParser {
161+
const PATH: &[rustc_span::Symbol] = &[sym::export_visibility];
162+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
163+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
164+
const ALLOWED_TARGETS: AllowedTargets =
165+
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::Static)]);
166+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "visibility");
167+
168+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
169+
let Some(nv) = args.name_value() else {
170+
cx.expected_name_value(cx.attr_span, None);
171+
return None;
172+
};
173+
let Some(sv) = nv.value_as_str() else {
174+
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
175+
return None;
176+
};
177+
178+
let str_to_visibility = [("target_default", ExportVisibilityAttrValue::TargetDefault)];
179+
for &(s, visibility) in str_to_visibility.iter() {
180+
if s == sv.as_str() {
181+
return Some(AttributeKind::ExportVisibility { visibility, span: cx.attr_span });
182+
}
183+
}
184+
185+
let allowed_str_values = str_to_visibility
186+
.into_iter()
187+
.map(|(s, _visibility)| s)
188+
.map(Symbol::intern)
189+
.collect::<Vec<_>>();
190+
cx.expected_specific_argument_strings(nv.value_span, &allowed_str_values);
191+
None
192+
}
193+
}
194+
156195
pub(crate) struct RustcObjcClassParser;
157196

158197
impl<S: Stage> SingleAttributeParser<S> for RustcObjcClassParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ attribute_parsers!(
184184
Single<DeprecatedParser>,
185185
Single<DoNotRecommendParser>,
186186
Single<ExportNameParser>,
187+
Single<ExportVisibilityParser>,
187188
Single<IgnoreParser>,
188189
Single<InlineParser>,
189190
Single<InstructionSetParser>,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_abi::{Align, ExternAbi};
22
use rustc_hir::attrs::{
3-
AttributeKind, EiiImplResolution, InlineAttr, Linkage, RtsanSetting, UsedBy,
3+
AttributeKind, EiiImplResolution, ExportVisibilityAttrValue, InlineAttr, Linkage, RtsanSetting,
4+
UsedBy,
45
};
56
use rustc_hir::def::DefKind;
67
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
@@ -70,6 +71,13 @@ fn process_builtin_attrs(
7071
match attr {
7172
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
7273
AttributeKind::ExportName { name, .. } => codegen_fn_attrs.symbol_name = Some(*name),
74+
AttributeKind::ExportVisibility { visibility, .. } => {
75+
codegen_fn_attrs.export_visibility = Some(match visibility {
76+
ExportVisibilityAttrValue::TargetDefault => {
77+
tcx.sess.default_visibility().into()
78+
}
79+
});
80+
}
7381
AttributeKind::Inline(inline, span) => {
7482
codegen_fn_attrs.inline = *inline;
7583
interesting_spans.inline = Some(*span);
@@ -533,6 +541,16 @@ fn handle_lang_items(
533541
}
534542
err.emit();
535543
}
544+
545+
if codegen_fn_attrs.export_visibility.is_some() {
546+
let span = find_attr!(attrs, ExportVisibility{span, ..} => *span).unwrap_or_default();
547+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
548+
tcx.dcx().emit_err(errors::ExportVisibilityWithRustcStdInternalSymbol { span });
549+
}
550+
if !codegen_fn_attrs.contains_extern_indicator() {
551+
tcx.dcx().emit_err(errors::ExportVisibilityWithoutNoMangleNorExportName { span });
552+
}
553+
}
536554
}
537555

538556
/// Generate the [`CodegenFnAttrs`] for an item (identified by the [`LocalDefId`]).

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,3 +1268,20 @@ pub(crate) struct LtoProcMacro;
12681268
#[diag("cannot prefer dynamic linking when performing LTO")]
12691269
#[note("only 'staticlib', 'bin', and 'cdylib' outputs are supported with LTO")]
12701270
pub(crate) struct DynamicLinkingWithLTO;
1271+
1272+
#[derive(Diagnostic)]
1273+
#[diag("`#[export_visibility = ...]` cannot be used on internal language items")]
1274+
pub(crate) struct ExportVisibilityWithRustcStdInternalSymbol {
1275+
#[primary_span]
1276+
pub span: Span,
1277+
}
1278+
1279+
#[derive(Diagnostic)]
1280+
#[diag(
1281+
"`#[export_visibility = ...]` will be ignored \
1282+
without `export_name`, `no_mangle`, or similar attribute"
1283+
)]
1284+
pub(crate) struct ExportVisibilityWithoutNoMangleNorExportName {
1285+
#[primary_span]
1286+
pub span: Span,
1287+
}

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
649649
template!(NameValueStr: "name", "https://doc.rust-lang.org/reference/abi.html#the-export_name-attribute"),
650650
FutureWarnPreceding, EncodeCrossCrate::No
651651
),
652+
gated!(export_visibility, Normal, template!(NameValueStr: "visibility"), ErrorPreceding, EncodeCrossCrate::No, experimental!(export_visibility)),
652653
ungated!(
653654
unsafe(Edition2024) link_section, Normal,
654655
template!(NameValueStr: "name", "https://doc.rust-lang.org/reference/abi.html#the-link_section-attribute"),

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ declare_features! (
474474
(incomplete, explicit_tail_calls, "1.72.0", Some(112788)),
475475
/// Allows using `#[export_stable]` which indicates that an item is exportable.
476476
(incomplete, export_stable, "1.88.0", Some(139939)),
477+
/// Allows `#[export_visibility]` on definitions of statics and/or functions.
478+
(unstable, export_visibility, "CURRENT_RUSTC_VERSION", Some(151425)),
477479
/// Externally implementable items
478480
(unstable, extern_item_impls, "1.94.0", Some(125418)),
479481
/// Allows defining `extern type`s.

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ impl Deprecation {
251251
}
252252
}
253253

254+
/// Pre-parsed value of `#[export_visibility = ...]` attribute.
255+
///
256+
/// In a future RFC we may consider adding support for `Hidden`, `Protected`, and/or
257+
/// `Interposable`.
258+
#[derive(Clone, Copy, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
259+
pub enum ExportVisibilityAttrValue {
260+
TargetDefault,
261+
}
262+
254263
/// There are three valid forms of the attribute:
255264
/// `#[used]`, which is equivalent to `#[used(linker)]` on targets that support it, but `#[used(compiler)]` if not.
256265
/// `#[used(compiler)]`
@@ -1045,6 +1054,12 @@ pub enum AttributeKind {
10451054
/// Represents `#[export_stable]`.
10461055
ExportStable,
10471056

1057+
/// Represents [`#[export_visibility = ...]`](https://github.com/rust-lang/rust/issues/151425)
1058+
ExportVisibility {
1059+
visibility: ExportVisibilityAttrValue,
1060+
span: Span,
1061+
},
1062+
10481063
/// Represents `#[feature(...)]`
10491064
Feature(ThinVec<Ident>, Span),
10501065

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl AttributeKind {
4343
EiiImpls(..) => No,
4444
ExportName { .. } => Yes,
4545
ExportStable => No,
46+
ExportVisibility { .. } => Yes,
4647
Feature(..) => No,
4748
FfiConst(..) => No,
4849
FfiPure(..) => No,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use rustc_index::IndexVec;
2+
use rustc_type_ir::RegionVid;
3+
4+
use crate::infer::SubregionOrigin;
5+
use crate::infer::region_constraints::{Constraint, ConstraintKind, RegionConstraintData};
6+
7+
/// Selects either out-edges or in-edges for [`IndexedConstraintEdges::adjacent_edges`].
8+
#[derive(Clone, Copy, Debug)]
9+
pub(super) enum EdgeDirection {
10+
Out,
11+
In,
12+
}
13+
14+
/// Type alias for the pairs stored in [`RegionConstraintData::constraints`],
15+
/// which we are indexing.
16+
type ConstraintPair<'tcx> = (Constraint<'tcx>, SubregionOrigin<'tcx>);
17+
18+
/// An index from region variables to their corresponding constraint edges,
19+
/// used on some error paths.
20+
pub(super) struct IndexedConstraintEdges<'data, 'tcx> {
21+
out_edges: IndexVec<RegionVid, Vec<&'data ConstraintPair<'tcx>>>,
22+
in_edges: IndexVec<RegionVid, Vec<&'data ConstraintPair<'tcx>>>,
23+
}
24+
25+
impl<'data, 'tcx> IndexedConstraintEdges<'data, 'tcx> {
26+
pub(super) fn build_index(num_vars: usize, data: &'data RegionConstraintData<'tcx>) -> Self {
27+
let mut out_edges = IndexVec::from_fn_n(|_| vec![], num_vars);
28+
let mut in_edges = IndexVec::from_fn_n(|_| vec![], num_vars);
29+
30+
for pair @ (c, _) in &data.constraints {
31+
// Only push a var out-edge for `VarSub...` constraints.
32+
match c.kind {
33+
ConstraintKind::VarSubVar | ConstraintKind::VarSubReg => {
34+
out_edges[c.sub.as_var()].push(pair)
35+
}
36+
ConstraintKind::RegSubVar | ConstraintKind::RegSubReg => {}
37+
}
38+
}
39+
40+
// Index in-edges in reverse order, to match what current tests expect.
41+
// (It's unclear whether this is important or not.)
42+
for pair @ (c, _) in data.constraints.iter().rev() {
43+
// Only push a var in-edge for `...SubVar` constraints.
44+
match c.kind {
45+
ConstraintKind::VarSubVar | ConstraintKind::RegSubVar => {
46+
in_edges[c.sup.as_var()].push(pair)
47+
}
48+
ConstraintKind::VarSubReg | ConstraintKind::RegSubReg => {}
49+
}
50+
}
51+
52+
IndexedConstraintEdges { out_edges, in_edges }
53+
}
54+
55+
/// Returns either the out-edges or in-edges of the specified region var,
56+
/// as selected by `dir`.
57+
pub(super) fn adjacent_edges(
58+
&self,
59+
region_vid: RegionVid,
60+
dir: EdgeDirection,
61+
) -> &[&'data ConstraintPair<'tcx>] {
62+
let edges = match dir {
63+
EdgeDirection::Out => &self.out_edges,
64+
EdgeDirection::In => &self.in_edges,
65+
};
66+
&edges[region_vid]
67+
}
68+
}

0 commit comments

Comments
 (0)