Skip to content
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

#[diagnostic::on_unimplemented] without filters #114452

Merged
merged 1 commit into from
Sep 17, 2023

Conversation

weiznich
Copy link
Contributor

@weiznich weiznich commented Aug 4, 2023

This commit adds support for a #[diagnostic::on_unimplemented] attribute with the following options:

  • message to customize the primary error message
  • note to add a customized note message to an error message
  • label to customize the label part of the error message

The relevant behavior is specified in RFC-3368

@rustbot
Copy link
Collaborator

rustbot commented Aug 4, 2023

r? @TaKO8Ki

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 4, 2023
@rust-log-analyzer

This comment has been minimized.

@weiznich weiznich force-pushed the feature/diagnostic_on_unimplemented branch from 8a50b1a to 8ffd5f6 Compare August 4, 2023 12:04
/// be available on newer compiler versions
pub MALFORMED_DIAGNOSTIC_ATTRIBUTES,
Warn,
"unrecognized diagnostic attribute options"
Copy link
Contributor

Choose a reason for hiding this comment

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

There are too many new lints for this feature, IMO.

Is it ever necessary to e.g. allow malformed diagnostic attributes, but warn on unknown?
I don't think so, this lints generally exists to warn on diagnostic attributes that are both known and well-formed, just on some other version of Rust compiler, so the distinction is not significant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also the previous lint was introduced as stable, but it should probably use a feature gate @feature_gate = sym::feature_gate_name; for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Will change that to use the other lint. We might consider renaming the lint then.

For the @feature_gate change: Is it ok to put that into this PR as well or should that be opened as a separate PR?

Copy link
Contributor Author

@weiznich weiznich Aug 10, 2023

Choose a reason for hiding this comment

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

I've pushed 9f51111 to address these concerns.

@weiznich weiznich mentioned this pull request Aug 8, 2023
@weiznich weiznich force-pushed the feature/diagnostic_on_unimplemented branch from c59ef01 to 9f51111 Compare August 10, 2023 05:41
@bors
Copy link
Contributor

bors commented Aug 15, 2023

☔ The latest upstream changes (presumably #114023) made this pull request unmergeable. Please resolve the merge conflicts.

@weiznich weiznich force-pushed the feature/diagnostic_on_unimplemented branch from 9f51111 to adcaef1 Compare August 18, 2023 09:05
@apiraino
Copy link
Contributor

r? compiler

@rustbot rustbot assigned compiler-errors and unassigned TaKO8Ki Aug 18, 2023
@bors
Copy link
Contributor

bors commented Aug 24, 2023

☔ The latest upstream changes (presumably #115094) made this pull request unmergeable. Please resolve the merge conflicts.

@weiznich weiznich force-pushed the feature/diagnostic_on_unimplemented branch from adcaef1 to fe719f9 Compare August 25, 2023 06:54
.segments
.iter()
.zip(name)
.all(|(s, n)| s.ident == Ident::with_dummy_span(*n))
Copy link
Member

Choose a reason for hiding this comment

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

This equality is weird, and relies on the fact that idents are equal only if their symbols are equal and their span contexts are identical:
https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_span/symbol.rs.html#1795-1800

If you're just trying to compare the symbols against name, then try

Suggested change
.all(|(s, n)| s.ident == Ident::with_dummy_span(*n))
.all(|(s, n)| s.ident.name == *n)

compiler/rustc_ast/src/attr/mod.rs Show resolved Hide resolved
Comment on lines 577 to 591
let def_kind = tcx.def_kind(id.owner_id);
if !matches!(def_kind, DefKind::Trait) {
let item_def_id = id.owner_id.to_def_id();
if let Some(attr) = tcx
.get_attrs_by_path(item_def_id, Box::new([sym::diagnostic, sym::on_unimplemented]))
.next()
{
tcx.emit_spanned_lint(
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local()),
vec![attr.span],
DiagnosticOnUnimplementedOnlyForTraits,
);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this check should live here. Attr validation better lives in compiler/rustc_passes/src/check_attr.rs or something like that.

pub fn get_attrs_by_path(
self,
did: impl Into<DefId>,
attr: Box<[Symbol]>,
Copy link
Member

Choose a reason for hiding this comment

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

Why does this take a Box? 😅

Please use lifetimes here and make it &'a [Symbol] -- the allocation here is really unnecessary.

@@ -2410,6 +2410,23 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

pub fn get_attrs_by_path(
self,
did: impl Into<DefId>,
Copy link
Member

Choose a reason for hiding this comment

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

Why does this take an Into<DefId>? It's really general -- please just take a DefId, and make the caller call into_def_id() if needed.

tcx.emit_spanned_lint(
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local()),
vec![attr.span],
Copy link
Member

Choose a reason for hiding this comment

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

Span is Into<MultiSpan>

Suggested change
vec![attr.span],
attr.span,

compiler/rustc_middle/src/ty/mod.rs Outdated Show resolved Hide resolved
@@ -401,15 +410,30 @@ impl<'tcx> OnUnimplementedDirective {
&& message.is_none()
&& label.is_none()
&& note.is_none()
&& !is_diagnostic_namespace_variant
// disallow filters for now
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// disallow filters for now
// FIXME(diagnostic_namespace): disallow filters for now

is_diagnostic_namespace_variant,
) {
Ok(Some(subcommand)) => subcommands.push(subcommand),
Ok(None) => unreachable!(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Ok(None) => unreachable!(
Ok(None) => bug!(

@compiler-errors
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 2, 2023
@weiznich
Copy link
Contributor Author

weiznich commented Sep 7, 2023

@compiler-errors Thanks for the review. These comments are really helpful. ❤️
I've pushed f5c87ba to address the comments.

@rustbot reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 7, 2023
compiler/rustc_middle/src/ty/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_analysis/src/check/check.rs Outdated Show resolved Hide resolved
compiler/rustc_passes/src/check_attr.rs Outdated Show resolved Hide resolved
tcx.emit_spanned_lint(
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local()),
vec![attr.span],
Copy link
Member

Choose a reason for hiding this comment

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

Please fix this here too.

tcx.emit_spanned_lint(
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local()),
vec![attr.span],
Copy link
Member

Choose a reason for hiding this comment

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

And here

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

Also please squash all of these "apply suggestions", "address suggestion", "update" commits. It's not super needed to retain them in git history, and I don't really review them individually. Thanks!

attr_span: Span,
hir_id: HirId,
target: Target,
) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

I also mentioned making this function not return bool.

@@ -284,6 +292,23 @@ impl CheckAttrVisitor<'_> {
}
}

/// Checks if `#[diagnostic::on_unimplemented[` is applied to a trait definition
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Checks if `#[diagnostic::on_unimplemented[` is applied to a trait definition
/// Checks if `#[diagnostic::on_unimplemented]` is applied to a trait definition

@@ -99,6 +99,22 @@ impl Attribute {
}
}

pub fn is_path(&self, name: &[Symbol]) -> bool {
Copy link
Member

@compiler-errors compiler-errors Sep 7, 2023

Choose a reason for hiding this comment

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

The name here is a bit weird, since it's inconsistent with is_word, which only allows #[word] and not #[word()]. Maybe let's name this path_matches or something?

@weiznich weiznich force-pushed the feature/diagnostic_on_unimplemented branch 3 times, most recently from e0d4bc0 to 3538130 Compare September 12, 2023 17:40
@rust-log-analyzer

This comment has been minimized.

This commit adds support for a `#[diagnostic::on_unimplemented]`
attribute with the following options:

* `message` to customize the primary error message
* `note` to add a customized note message to an error message
* `label` to customize the label part of the error message

Co-authored-by: León Orell Valerian Liehr <me@fmease.dev>
Co-authored-by: Michael Goulet <michael@errs.io>
@weiznich weiznich force-pushed the feature/diagnostic_on_unimplemented branch from 3538130 to 5b8a7a0 Compare September 12, 2023 18:03
@weiznich
Copy link
Contributor Author

@compiler-errors This PR is still marked as waiting-on-review. Is there anything that I can to do move it forward?

@compiler-errors
Copy link
Member

@weiznich: please be patient, it's been less than 24 hours since you last updated it, as far as I can tell. I haven't had the time to review the changes, and probably will not for a few days.

@weiznich
Copy link
Contributor Author

I'm mainly asking because you did not remove the waiting-on-review flag after your last review. Its unclear for me whether you've finished that one or if I should expect more comments from that review round.

@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Sep 16, 2023

📌 Commit 5b8a7a0 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 16, 2023
@bors
Copy link
Contributor

bors commented Sep 17, 2023

⌛ Testing commit 5b8a7a0 with merge 327e6cf...

@bors
Copy link
Contributor

bors commented Sep 17, 2023

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing 327e6cf to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 17, 2023
@bors bors merged commit 327e6cf into rust-lang:master Sep 17, 2023
12 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Sep 17, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (327e6cf): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.4%, 0.9%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.9% [4.9%, 4.9%] 1
Improvements ✅
(primary)
-2.1% [-2.1%, -2.1%] 1
Improvements ✅
(secondary)
-3.5% [-6.2%, -1.0%] 5
All ❌✅ (primary) -2.1% [-2.1%, -2.1%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 636.485s -> 634.734s (-0.28%)
Artifact size: 318.24 MiB -> 318.52 MiB (0.09%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants