Skip to content

Encode hir attributes cross-crate properly #142777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3724,6 +3724,7 @@ dependencies = [
name = "rustc_feature"
version = "0.0.0"
dependencies = [
"rustc_attr_data_structures",
"rustc_data_structures",
"rustc_span",
"serde",
Expand Down
31 changes: 31 additions & 0 deletions compiler/rustc_attr_data_structures/src/encode_cross_crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::AttributeKind;

#[derive(PartialEq)]
pub enum EncodeCrossCrate {
Yes,
No,
}

impl AttributeKind {
pub fn encode_cross_crate(&self) -> EncodeCrossCrate {
use AttributeKind::*;
use EncodeCrossCrate::*;

match self {
Align { .. } => No,
AllowConstFnUnstable(..) => No,
AllowInternalUnstable(..) => Yes,
AsPtr(..) => Yes,
BodyStability { .. } => No,
Confusables { .. } => Yes,
ConstStability { .. } => Yes,
ConstStabilityIndirect => No,
Deprecation { .. } => Yes,
DocComment { .. } => Yes,
Inline(..) => No,
MacroTransparency(..) => Yes,
Repr(..) => No,
Stability { .. } => Yes,
}
}
}
2 changes: 2 additions & 0 deletions compiler/rustc_attr_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// tidy-alphabetical-end

mod attributes;
mod encode_cross_crate;
mod stability;
mod version;

Expand All @@ -17,6 +18,7 @@ pub mod lints;
use std::num::NonZero;

pub use attributes::*;
pub use encode_cross_crate::EncodeCrossCrate;
use rustc_abi::Align;
use rustc_ast::token::CommentKind;
use rustc_ast::{AttrStyle, IntTy, UintTy};
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_feature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ edition = "2024"

[dependencies]
# tidy-alphabetical-start
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_span = { path = "../rustc_span" }
serde = { version = "1.0.125", features = [ "derive" ] }
serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.59"
# tidy-alphabetical-end
7 changes: 1 addition & 6 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::LazyLock;
use AttributeDuplicates::*;
use AttributeGate::*;
use AttributeType::*;
use rustc_attr_data_structures::EncodeCrossCrate;
use rustc_data_structures::fx::FxHashMap;
use rustc_span::edition::Edition;
use rustc_span::{Symbol, sym};
Expand Down Expand Up @@ -368,12 +369,6 @@ macro_rules! experimental {
};
}

#[derive(PartialEq)]
pub enum EncodeCrossCrate {
Yes,
No,
}

pub struct BuiltinAttribute {
pub name: Symbol,
/// Whether this attribute is encode cross crate.
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use rustc_ast::attr::AttributeExt;
use rustc_attr_data_structures::EncodeCrossCrate;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::memmap::{Mmap, MmapMut};
use rustc_data_structures::sync::{join, par_for_each_in};
Expand Down Expand Up @@ -824,9 +824,13 @@ struct AnalyzeAttrState<'a> {
/// visibility: this is a piece of data that can be computed once per defid, and not once per
/// attribute. Some attributes would only be usable downstream if they are public.
#[inline]
fn analyze_attr(attr: &impl AttributeExt, state: &mut AnalyzeAttrState<'_>) -> bool {
fn analyze_attr(attr: &hir::Attribute, state: &mut AnalyzeAttrState<'_>) -> bool {
let mut should_encode = false;
if let Some(name) = attr.name()
if let hir::Attribute::Parsed(p) = attr
&& p.encode_cross_crate() == EncodeCrossCrate::No
{
// Attributes not marked encode-cross-crate don't need to be encoded for downstream crates.
} else if let Some(name) = attr.name()
&& !rustc_feature::encode_cross_crate(name)
{
// Attributes not marked encode-cross-crate don't need to be encoded for downstream crates.
Expand Down
Loading