Repository navigation
Move check_rustc_must_implement_one_of validation to attribute parser #163980
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| use rustc_ast::{GenericParamKind, ItemKind, LitIntType, LitKind, MetaItemLit}; | ||
| use rustc_ast::{AssocItemKind, GenericParamKind, ItemKind, LitIntType, LitKind, MetaItemLit}; | ||
| use rustc_attr_ir::lang_items::LangItem; | ||
| use rustc_attr_ir::{ | ||
| BorrowckGraphvizFormatKind, CguFields, CguKind, RustcAssertVarianceKind, RustcCleanAttribute, | ||
| RustcCleanQueries, RustcMirKind, | ||
| RustcCleanQueries, RustcMirKind, find_attr, | ||
| }; | ||
| use rustc_data_structures::fx::FxHashMap; | ||
| use rustc_feature::AttributeStability; | ||
|
|
@@ -86,6 +86,51 @@ impl SingleAttributeParser for RustcMustImplementOneOfParser { | |
|
|
||
| Some(AttributeKind::RustcMustImplementOneOf { attr_span: cx.attr_span, fn_names }) | ||
| } | ||
|
|
||
| fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { | ||
| if cx.target != Target::Trait { | ||
| return; | ||
| } | ||
|
|
||
| let item = cx.target_item.expect("missing AST target item for Target::Trait"); | ||
| let ItemKind::Trait(trait_) = &item.kind else { | ||
| panic!("expected trait AST target item for Target::Trait"); | ||
| }; | ||
|
|
||
| let fn_names = | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kept the full Added a couple of macro tests to make sure the hygiene behavior stays the same. |
||
| find_attr!(cx.parsed_attrs, RustcMustImplementOneOf { fn_names, .. } => fn_names) | ||
| .expect("missing parsed RustcMustImplementOneOf attribute in finalize_check"); | ||
|
|
||
| for ident in fn_names { | ||
| let item = trait_.items.iter().find(|item| item.kind.ident() == Some(*ident)); | ||
|
|
||
| match item { | ||
| Some(item) => match &item.kind { | ||
| AssocItemKind::Fn(f) => { | ||
| if f.body.is_none() { | ||
| cx.emit_err(diagnostics::FunctionNotHaveDefaultImplementation { | ||
| span: item.span, | ||
| note_span: attr_span, | ||
| }); | ||
| } | ||
| } | ||
| AssocItemKind::Delegation(_) => {} | ||
| _ => { | ||
| cx.emit_err(diagnostics::MustImplementNotFunction { | ||
| span: item.span, | ||
| span_note: diagnostics::MustImplementNotFunctionSpanNote { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping delegation as a valid case here since it lowers to I added tests for both normal and renamed delegation as well. |
||
| span: attr_span, | ||
| }, | ||
| note: diagnostics::MustImplementNotFunctionNote {}, | ||
| }); | ||
| } | ||
| }, | ||
| None => { | ||
| cx.emit_err(diagnostics::FunctionNotFoundInTrait { span: ident.span }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct RustcNeverReturnsNullPtrParser; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| //@ check-pass | ||
|
|
||
| #![feature(rustc_attrs)] | ||
| #![feature(fn_delegation)] | ||
|
|
||
| mod to_reuse { | ||
| pub fn b() {} | ||
| pub fn c() {} | ||
| } | ||
|
|
||
| #[rustc_must_implement_one_of(a, b)] | ||
| trait TraitDelegation { | ||
| fn a() {} | ||
| reuse to_reuse::b; | ||
| } | ||
|
|
||
| #[rustc_must_implement_one_of(a, ren)] | ||
| trait TraitDelegationRenamed { | ||
| fn a() {} | ||
| reuse to_reuse::c as ren; | ||
| } | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #![feature(rustc_attrs)] | ||
|
|
||
| macro_rules! define_method { | ||
| ($name:ident) => { | ||
| fn $name() {} | ||
| }; | ||
| } | ||
|
|
||
| #[rustc_must_implement_one_of(a, b)] | ||
| trait TraitMacro { | ||
| fn a() {} | ||
| define_method!(b); | ||
| } | ||
|
|
||
| macro_rules! define_trait { | ||
| ($trait_name:ident, $m1:ident, $m2:ident) => { | ||
| #[rustc_must_implement_one_of($m1, $m2)] | ||
| trait $trait_name { | ||
| fn $m1() {} | ||
| fn $m2() {} | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| define_trait!(TraitMacroWhole, foo, bar); | ||
|
|
||
| macro_rules! define_internal_b { | ||
| () => { | ||
| fn b() {} | ||
| }; | ||
| } | ||
|
|
||
| #[rustc_must_implement_one_of(a, b)] | ||
| //~^ ERROR function not found in this trait | ||
| trait TraitHygiene { | ||
| fn a() {} | ||
| define_internal_b!(); | ||
| } | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| error: function not found in this trait | ||
| --> $DIR/rustc_must_implement_one_of_macros.rs:33:34 | ||
| | | ||
| LL | #[rustc_must_implement_one_of(a, b)] | ||
| | ^ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old
replace_with_dummywas replacing the trait items withMacCallplaceholders, sofinalize_checkcouldn't actually inspect them throughtarget_item.This keeps the original items in the trait while still indexing the cloned ones separately for lowering.
The downside is that we're cloning trait items even when the attribute isn't used. I couldn't find a simpler way to reuse the existing infrastructure here, but I'm open to suggestions.
View changes since the review
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit: (This approach is suggested by llm, since i couldn't come with a better/simplied solution) Maybe another option could be to keep the original trait items only for traits with
#[rustc_must_implement_one_of], and use the existing placeholders everywhere else. That would still involve cloning, but limit it to traits that actually need the check. Not sure how neatly it would fit into the indexer, especially with nested traits, but it might be worth exploring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both options might be terrible for performance, let's check.
It also just doesn't feel like a great solution, it might be better to just keep the check in
rustc_passesif this is neededThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've started a small experiment here that might make this code nice if perf allows: #164980
I'm not expecting that PR to be accepted tho
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried this with #163980 locally and it works without cloning the trait items. I just dropped the cloning hunk and kept your indexer as-is. Thank you!!!
Edit: heres the branch if you'd like to inspect https://github.com/darkraider01/rust/tree/experiment-ast-retention-164980
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly the perf effect of #164980 is not acceptable