Skip to content
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
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
attribute_parser: AttributeParser::new(
tcx.sess,
tcx.features(),
tcx.registered_tools(()),
tcx.registered_attribute_tools(()),
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
),
delayed_lints: Vec::new(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl AttributeParser for NakedParser {

let span = self.span?;

let Some(tools) = cx.tools else {
let Some(tools) = cx.attribute_tools else {
unreachable!("tools required while parsing attributes");
};

Expand Down
29 changes: 25 additions & 4 deletions compiler/rustc_attr_parsing/src/attributes/crate_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,33 @@ impl CombineAttributeParser for FeatureParser {
}
}

pub(crate) struct RegisterToolParser;
pub(crate) struct RegisterAttributeTool;
pub(crate) struct RegisterLintTool;
pub(crate) struct RegisterTool;

impl CombineAttributeParser for RegisterToolParser {
const PATH: &[Symbol] = &[sym::register_tool];
pub(crate) trait RegisterToolKind: 'static {
const SYMBOL: Symbol;
}

impl RegisterToolKind for RegisterAttributeTool {
const SYMBOL: Symbol = sym::register_attribute_tool;
}

impl RegisterToolKind for RegisterLintTool {
const SYMBOL: Symbol = sym::register_lint_tool;
}

impl RegisterToolKind for RegisterTool {
const SYMBOL: Symbol = sym::register_tool;
}

pub(crate) struct RegisterToolParser<Kind>(Kind);

impl<K: RegisterToolKind> CombineAttributeParser for RegisterToolParser<K> {
const PATH: &[Symbol] = &[K::SYMBOL];
type Item = Ident;
const CONVERT: ConvertFn<Self::Item> = |tools, _span| AttributeKind::RegisterTool(tools);
const CONVERT: ConvertFn<Self::Item> =
|tools, _span| AttributeKind::RegisterTool { kind: K::SYMBOL, tools };
Comment on lines +320 to +346

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this would need to implement AttributeParser directly. For example see the stability parser:

impl AttributeParser for StabilityParser {
which combines #[stable], #[unstable] and #[rustc_allowed_through_unstable_modules] into a single semantic attribute.

const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
const TEMPLATE: AttributeTemplate = template!(List: &["tool1, tool2, ..."]);
const STABILITY: AttributeStability = unstable!(register_tool);
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ attribute_parsers!(
Combine<FeatureParser>,
Combine<ForceTargetFeatureParser>,
Combine<LinkParser>,
Combine<RegisterToolParser>,
Combine<RegisterToolParser<RegisterAttributeTool>>,
Combine<RegisterToolParser<RegisterLintTool>>,
Combine<RegisterToolParser<RegisterTool>>,
Combine<ReprParser>,
Combine<RustcAllowConstFnUnstableParser>,
Combine<RustcCleanParser>,
Expand Down
27 changes: 17 additions & 10 deletions compiler/rustc_attr_parsing/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ pub struct EmitAttribute(
/// Context created once, for example as part of the ast lowering
/// context, through which all attributes can be lowered.
pub struct AttributeParser<'sess> {
pub(crate) tools: Option<&'sess RegisteredTools>,
pub(crate) attribute_tools: Option<&'sess RegisteredTools>,
pub(crate) features: Option<&'sess Features>,
pub(crate) sess: &'sess Session,
pub(crate) should_emit: ShouldEmit,

/// *Only* parse attributes with this symbol.
///
/// Used in cases where we want the lowering infrastructure for parse just a single attribute.
parse_only: Option<&'static [Symbol]>,
parse_only: Option<&'sess [Symbol]>,
}

impl<'sess> AttributeParser<'sess> {
Expand All @@ -68,7 +68,7 @@ impl<'sess> AttributeParser<'sess> {
pub fn parse_limited(
sess: &'sess Session,
attrs: &[ast::Attribute],
sym: &'static [Symbol],
sym: &'sess [Symbol],
) -> Option<Attribute> {
Self::parse_limited_should_emit(
sess,
Expand All @@ -90,7 +90,7 @@ impl<'sess> AttributeParser<'sess> {
pub fn parse_limited_should_emit(
sess: &'sess Session,
attrs: &[ast::Attribute],
sym: &'static [Symbol],
sym: &'sess [Symbol],
target_span: Span,
target_node_id: NodeId,
target: Target,
Expand Down Expand Up @@ -122,15 +122,15 @@ impl<'sess> AttributeParser<'sess> {
pub fn parse_limited_all(
sess: &'sess Session,
attrs: &[ast::Attribute],
parse_only: Option<&'static [Symbol]>,
parse_only: Option<&'sess [Symbol]>,
target: Target,
target_span: Span,
target_node_id: NodeId,
features: Option<&'sess Features>,
should_emit: ShouldEmit,
tools: Option<&'sess RegisteredTools>,
attribute_tools: Option<&'sess RegisteredTools>,
) -> Vec<Attribute> {
let mut p = Self { features, tools, parse_only, sess, should_emit };
let mut p = Self { features, attribute_tools, parse_only, sess, should_emit };
p.parse_attribute_list(
attrs,
target_span,
Expand Down Expand Up @@ -212,7 +212,8 @@ impl<'sess> AttributeParser<'sess> {
parse_fn: fn(cx: &mut AcceptContext<'_, '_>, item: &I) -> T,
template: &AttributeTemplate,
) -> T {
let mut parser = Self { features, tools: None, parse_only: None, sess, should_emit };
let mut parser =
Self { features, attribute_tools: None, parse_only: None, sess, should_emit };
let mut emit_lint = |lint_id: LintId, span: MultiSpan, kind: EmitAttribute| {
sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0)
};
Expand Down Expand Up @@ -252,10 +253,16 @@ impl<'sess> AttributeParser<'sess> {
pub fn new(
sess: &'sess Session,
features: &'sess Features,
tools: &'sess RegisteredTools,
attribute_tools: &'sess RegisteredTools,
should_emit: ShouldEmit,
) -> Self {
Self { features: Some(features), tools: Some(tools), parse_only: None, sess, should_emit }
Self {
features: Some(features),
attribute_tools: Some(attribute_tools),
parse_only: None,
sess,
should_emit,
}
}

pub(crate) fn sess(&self) -> &'sess Session {
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use rustc_session::lint::{Lint, LintId};
use rustc_session::output::invalid_output_for_target;
use rustc_session::{EarlyDiagCtxt, Session, config};
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::{DUMMY_SP, FileName};
use rustc_span::{DUMMY_SP, FileName, sym};
use rustc_target::json::ToJson;
use rustc_target::spec::{Target, TargetTuple};
use tracing::trace;
Expand Down Expand Up @@ -713,13 +713,18 @@ fn print_crate_info(
let crate_name = passes::get_crate_name(sess, attrs);
let lint_store = crate::unerased_lint_store(sess);
let features = rustc_expand::config::features(sess, attrs, crate_name);
let registered_tools = rustc_resolve::registered_tools_ast(sess.dcx(), attrs, sess);
let registered_lint_tools = rustc_resolve::registered_tools_ast(
sess.dcx(),
attrs,
sess,
sym::register_lint_tool,
);
let builder = rustc_lint::LintLevelsBuilder::crate_root(
sess,
&features,
true,
lint_store,
&registered_tools,
&registered_lint_tools,
attrs,
);
for lint in lint_store.get_lints() {
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,8 +1161,11 @@ pub trait ResolverExpand {
cfg_span: Span,
);

/// Tools registered with `#![register_tool]` and used by tool attributes and lints.
fn registered_tools(&self) -> &RegisteredTools;
/// Tools registered with `#![register_tool]` or `#![register_attribute_tool]`.
fn registered_attribute_tools(&self) -> &RegisteredTools;

/// Tools registered with `#![register_tool]` or `#![register_lint_tool]`.
fn registered_lint_tools(&self) -> &RegisteredTools;

/// Mark this invocation id as a glob delegation.
fn register_glob_delegation(&mut self, invoc_id: LocalExpnId);
Expand All @@ -1189,7 +1192,7 @@ pub trait LintStoreExpand {
&self,
sess: &Session,
features: &Features,
registered_tools: &RegisteredTools,
registered_lint_tools: &RegisteredTools,
node_id: NodeId,
attrs: &[Attribute],
items: &[Box<Item>],
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ impl InvocationCollectorNode for Box<ast::Item> {
lint_store.pre_expansion_lint(
ecx.sess,
ecx.ecfg.features,
ecx.resolver.registered_tools(),
ecx.resolver.registered_lint_tools(),
ecx.current_expansion.lint_node_id,
&attrs,
&items,
Expand Down Expand Up @@ -2268,7 +2268,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
self.cx.current_expansion.lint_node_id,
Some(self.cx.ecfg.features),
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
Some(self.cx.resolver.registered_tools()),
Some(self.cx.resolver.registered_attribute_tools()),
);

let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ pub static BUILTIN_ATTRIBUTES: &[Symbol] = &[

sym::ffi_pure,
sym::ffi_const,
sym::register_attribute_tool,
sym::register_lint_tool,
sym::register_tool,
// `#[cfi_encoding = ""]`
sym::cfi_encoding,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ declare_features! (
(removed, crate_visibility_modifier, "1.63.0", Some(53120), Some("removed in favor of `pub(crate)`"), 97254),
/// Allows using custom attributes (RFC 572).
(removed, custom_attribute, "1.0.0", Some(29642),
Some("removed in favor of `#![register_tool]` and `#![register_attr]`"), 66070),
Some("removed in favor of `#![register_tool]`"), 66070),
/// Allows the use of `#[derive(Anything)]` as sugar for `#[derive_Anything]`.
(removed, custom_derive, "1.32.0", Some(29644),
Some("subsumed by `#[proc_macro_derive]`")),
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,10 @@ pub enum AttributeKind {
ReexportTestHarnessMain(Symbol),

/// Represents `#[register_tool]`
RegisterTool(ThinVec<Ident>),
RegisterTool {
kind: Symbol,
tools: ThinVec<Ident>,
},
Comment on lines +1269 to +1272

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I would expect this to look like...

Suggested change
RegisterTool {
kind: Symbol,
tools: ThinVec<Ident>,
},
RegisterTool {
lint_tools: ThinVec<Ident>,
attribute_tools: ThinVec<Ident>,
},

and if you use register_tool the name would be put into both of those.


/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
Repr {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/attrs/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl AttributeKind {
ProfilerRuntime => No,
RecursionLimit { .. } => No,
ReexportTestHarnessMain(..) => No,
RegisterTool(..) => No,
RegisterTool { .. } => No,
Repr { .. } => No,
RustcAbi { .. } => No,
RustcAlign { .. } => No,
Expand Down
22 changes: 15 additions & 7 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn pre_expansion_lint<'a>(
sess: &Session,
features: &Features,
lint_store: &LintStore,
registered_tools: &RegisteredTools,
registered_lint_tools: &RegisteredTools,
check_node: impl EarlyCheckNode<'a>,
node_name: Symbol,
) {
Expand All @@ -99,7 +99,7 @@ fn pre_expansion_lint<'a>(
features,
true,
lint_store,
registered_tools,
registered_lint_tools,
None,
rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
check_node,
Expand All @@ -116,13 +116,20 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> {
&self,
sess: &Session,
features: &Features,
registered_tools: &RegisteredTools,
registered_lint_tools: &RegisteredTools,
node_id: ast::NodeId,
attrs: &[ast::Attribute],
items: &[Box<ast::Item>],
name: Symbol,
) {
pre_expansion_lint(sess, features, self.0, registered_tools, (node_id, attrs, items), name);
pre_expansion_lint(
sess,
features,
self.0,
registered_lint_tools,
(node_id, attrs, items),
name,
);
}
}

Expand All @@ -146,7 +153,7 @@ fn configure_and_expand(
sess,
features,
lint_store,
tcx.registered_tools(()),
tcx.registered_lint_tools(()),
lint_check_node,
crate_name,
);
Expand Down Expand Up @@ -477,7 +484,7 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
tcx.features(),
false,
lint_store,
tcx.registered_tools(()),
tcx.registered_lint_tools(()),
Some(lint_buffer),
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
(&*krate, &*krate.attrs),
Expand Down Expand Up @@ -794,7 +801,8 @@ fn resolver_for_lowering_raw<'tcx>(
&'tcx ty::ResolverGlobalCtxt,
) {
let arenas = Resolver::arenas();
let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
let _ = tcx.registered_attribute_tools(()); // Uses `crate_for_resolver`.
let _ = tcx.registered_lint_tools(()); // Uses `crate_for_resolver`.
let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
let mut resolver = Resolver::new(
tcx,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,13 @@ impl LintStore {
&self,
lint_name: &str,
tool_name: Option<Symbol>,
registered_tools: &RegisteredTools,
registered_lint_tools: &RegisteredTools,
) -> CheckLintNameResult<'_> {
if let Some(tool_name) = tool_name {
// FIXME: rustc and rustdoc are considered tools for lints, but not for attributes.
if tool_name != sym::rustc
&& tool_name != sym::rustdoc
&& !registered_tools.contains(&Ident::with_dummy_span(tool_name))
&& !registered_lint_tools.contains(&Ident::with_dummy_span(tool_name))
{
return CheckLintNameResult::NoTool;
}
Expand Down Expand Up @@ -573,7 +573,7 @@ impl<'a> EarlyContext<'a> {
features: &'a Features,
lint_added_lints: bool,
lint_store: &'a LintStore,
registered_tools: &'a RegisteredTools,
registered_lint_tools: &'a RegisteredTools,
buffered: LintBuffer,
) -> EarlyContext<'a> {
EarlyContext {
Expand All @@ -582,7 +582,7 @@ impl<'a> EarlyContext<'a> {
features,
lint_added_lints,
lint_store,
registered_tools,
registered_lint_tools,
),
buffered,
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub fn check_ast_node<'a>(
features: &Features,
pre_expansion: bool,
lint_store: &LintStore,
registered_tools: &RegisteredTools,
registered_lint_tools: &RegisteredTools,
lint_buffer: Option<LintBuffer>,
builtin_lints: impl EarlyLintPass + 'static,
check_node: impl EarlyCheckNode<'a>,
Expand All @@ -324,7 +324,7 @@ pub fn check_ast_node<'a>(
features,
!pre_expansion,
lint_store,
registered_tools,
registered_lint_tools,
lint_buffer.unwrap_or_default(),
);

Expand Down
Loading
Loading