Skip to content

Commit

Permalink
Auto merge of rust-lang#125326 - weiznich:move/do_not_recommend_to_di…
Browse files Browse the repository at this point in the history
…ganostic_namespace, r=<try>

Move `#[do_not_recommend]` to the `#[diagnostic]` namespace

This commit moves the `#[do_not_recommend]` attribute to the `#[diagnostic]` namespace. It still requires
`#![feature(do_not_recommend)]` to work.

r? `@compiler-errors`
  • Loading branch information
bors committed May 20, 2024
2 parents 44d679b + 7bdea9a commit 347462c
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 209 deletions.
10 changes: 10 additions & 0 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,20 @@ impl Attribute {
AttrKind::DocComment(..) => None,
}
}

pub fn name_or_empty(&self) -> Symbol {
self.ident().unwrap_or_else(Ident::empty).name
}

pub fn path(&self) -> Vec<Symbol> {
match &self.kind {
AttrKind::Normal(normal) => {
normal.item.path.segments.iter().map(|s| s.ident.name).collect()
}
AttrKind::DocComment(..) => vec![sym::doc],
}
}

#[inline]
pub fn has_name(&self, name: Symbol) -> bool {
match &self.kind {
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
EncodeCrossCrate::Yes, experimental!(deprecated_safe),
),

// RFC 2397
gated!(
do_not_recommend, Normal, template!(Word), WarnFollowing,
EncodeCrossCrate::Yes, experimental!(do_not_recommend)
),

// `#[cfi_encoding = ""]`
gated!(
cfi_encoding, Normal, template!(NameValueStr: "encoding"), ErrorPreceding,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ passes_implied_feature_not_exist =
feature `{$implied_by}` implying `{$feature}` does not exist
passes_incorrect_do_not_recommend_location =
`#[do_not_recommend]` can only be placed on trait implementations
`#[diagnostic::do_not_recommend]` can only be placed on trait implementations
passes_incorrect_meta_item = expected a quoted string literal
passes_incorrect_meta_item_suggestion = consider surrounding this with quotes
Expand Down
137 changes: 75 additions & 62 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,92 +116,96 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
let mut seen = FxHashMap::default();
let attrs = self.tcx.hir().attrs(hir_id);
for attr in attrs {
if attr.path_matches(&[sym::diagnostic, sym::on_unimplemented]) {
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target);
}
match attr.name_or_empty() {
sym::do_not_recommend => self.check_do_not_recommend(attr.span, target),
sym::inline => self.check_inline(hir_id, attr, span, target),
sym::coverage => self.check_coverage(hir_id, attr, span, target),
sym::non_exhaustive => self.check_non_exhaustive(hir_id, attr, span, target),
sym::marker => self.check_marker(hir_id, attr, span, target),
sym::target_feature => self.check_target_feature(hir_id, attr, span, target, attrs),
sym::thread_local => self.check_thread_local(attr, span, target),
sym::track_caller => {
match attr.path().as_slice() {
[sym::diagnostic, sym::do_not_recommend] => {
self.check_do_not_recommend(attr.span, hir_id, target)
}
[sym::diagnostic, sym::on_unimplemented] => {
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
}
[sym::inline] => self.check_inline(hir_id, attr, span, target),
[sym::coverage] => self.check_coverage(hir_id, attr, span, target),
[sym::non_exhaustive] => self.check_non_exhaustive(hir_id, attr, span, target),
[sym::marker] => self.check_marker(hir_id, attr, span, target),
[sym::target_feature] => {
self.check_target_feature(hir_id, attr, span, target, attrs)
}
[sym::thread_local] => self.check_thread_local(attr, span, target),
[sym::track_caller] => {
self.check_track_caller(hir_id, attr.span, attrs, span, target)
}
sym::doc => self.check_doc_attrs(
[sym::doc] => self.check_doc_attrs(
attr,
hir_id,
target,
&mut specified_inline,
&mut doc_aliases,
),
sym::no_link => self.check_no_link(hir_id, attr, span, target),
sym::export_name => self.check_export_name(hir_id, attr, span, target),
sym::rustc_layout_scalar_valid_range_start
| sym::rustc_layout_scalar_valid_range_end => {
[sym::no_link] => self.check_no_link(hir_id, attr, span, target),
[sym::export_name] => self.check_export_name(hir_id, attr, span, target),
[sym::rustc_layout_scalar_valid_range_start]
| [sym::rustc_layout_scalar_valid_range_end] => {
self.check_rustc_layout_scalar_valid_range(attr, span, target)
}
sym::allow_internal_unstable => {
[sym::allow_internal_unstable] => {
self.check_allow_internal_unstable(hir_id, attr, span, target, attrs)
}
sym::debugger_visualizer => self.check_debugger_visualizer(attr, target),
sym::rustc_allow_const_fn_unstable => {
[sym::debugger_visualizer] => self.check_debugger_visualizer(attr, target),
[sym::rustc_allow_const_fn_unstable] => {
self.check_rustc_allow_const_fn_unstable(hir_id, attr, span, target)
}
sym::rustc_std_internal_symbol => {
[sym::rustc_std_internal_symbol] => {
self.check_rustc_std_internal_symbol(attr, span, target)
}
sym::naked => self.check_naked(hir_id, attr, span, target),
sym::rustc_never_returns_null_ptr => {
[sym::naked] => self.check_naked(hir_id, attr, span, target),
[sym::rustc_never_returns_null_ptr] => {
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
}
sym::rustc_legacy_const_generics => {
[sym::rustc_legacy_const_generics] => {
self.check_rustc_legacy_const_generics(hir_id, attr, span, target, item)
}
sym::rustc_lint_query_instability => {
[sym::rustc_lint_query_instability] => {
self.check_rustc_lint_query_instability(hir_id, attr, span, target)
}
sym::rustc_lint_diagnostics => {
[sym::rustc_lint_diagnostics] => {
self.check_rustc_lint_diagnostics(hir_id, attr, span, target)
}
sym::rustc_lint_opt_ty => self.check_rustc_lint_opt_ty(attr, span, target),
sym::rustc_lint_opt_deny_field_access => {
[sym::rustc_lint_opt_ty] => self.check_rustc_lint_opt_ty(attr, span, target),
[sym::rustc_lint_opt_deny_field_access] => {
self.check_rustc_lint_opt_deny_field_access(attr, span, target)
}
sym::rustc_clean
| sym::rustc_dirty
| sym::rustc_if_this_changed
| sym::rustc_then_this_would_need => self.check_rustc_dirty_clean(attr),
sym::rustc_coinductive
| sym::rustc_must_implement_one_of
| sym::rustc_deny_explicit_impl
| sym::const_trait => self.check_must_be_applied_to_trait(attr, span, target),
sym::cmse_nonsecure_entry => {
[sym::rustc_clean]
| [sym::rustc_dirty]
| [sym::rustc_if_this_changed]
| [sym::rustc_then_this_would_need] => self.check_rustc_dirty_clean(attr),
[sym::rustc_coinductive]
| [sym::rustc_must_implement_one_of]
| [sym::rustc_deny_explicit_impl]
| [sym::const_trait] => self.check_must_be_applied_to_trait(attr, span, target),
[sym::cmse_nonsecure_entry] => {
self.check_cmse_nonsecure_entry(hir_id, attr, span, target)
}
sym::collapse_debuginfo => self.check_collapse_debuginfo(attr, span, target),
sym::must_not_suspend => self.check_must_not_suspend(attr, span, target),
sym::must_use => self.check_must_use(hir_id, attr, target),
sym::rustc_pass_by_value => self.check_pass_by_value(attr, span, target),
sym::rustc_allow_incoherent_impl => {
[sym::collapse_debuginfo] => self.check_collapse_debuginfo(attr, span, target),
[sym::must_not_suspend] => self.check_must_not_suspend(attr, span, target),
[sym::must_use] => self.check_must_use(hir_id, attr, target),
[sym::rustc_pass_by_value] => self.check_pass_by_value(attr, span, target),
[sym::rustc_allow_incoherent_impl] => {
self.check_allow_incoherent_impl(attr, span, target)
}
sym::rustc_has_incoherent_inherent_impls => {
[sym::rustc_has_incoherent_inherent_impls] => {
self.check_has_incoherent_inherent_impls(attr, span, target)
}
sym::ffi_pure => self.check_ffi_pure(attr.span, attrs, target),
sym::ffi_const => self.check_ffi_const(attr.span, target),
sym::rustc_const_unstable
| sym::rustc_const_stable
| sym::unstable
| sym::stable
| sym::rustc_allowed_through_unstable_modules
| sym::rustc_promotable => self.check_stability_promotable(attr, target),
sym::link_ordinal => self.check_link_ordinal(attr, span, target),
sym::rustc_confusables => self.check_confusables(attr, target),
sym::rustc_safe_intrinsic => {
[sym::ffi_pure] => self.check_ffi_pure(attr.span, attrs, target),
[sym::ffi_const] => self.check_ffi_const(attr.span, target),
[sym::rustc_const_unstable]
| [sym::rustc_const_stable]
| [sym::unstable]
| [sym::stable]
| [sym::rustc_allowed_through_unstable_modules]
| [sym::rustc_promotable] => self.check_stability_promotable(attr, target),
[sym::link_ordinal] => self.check_link_ordinal(attr, span, target),
[sym::rustc_confusables] => self.check_confusables(attr, target),
[sym::rustc_safe_intrinsic] => {
self.check_rustc_safe_intrinsic(hir_id, attr, span, target)
}
_ => true,
Expand Down Expand Up @@ -293,18 +297,26 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
);
}

/// Checks if `#[do_not_recommend]` is applied on a trait impl.
fn check_do_not_recommend(&self, attr_span: Span, target: Target) -> bool {
if let Target::Impl = target {
true
} else {
self.dcx().emit_err(errors::IncorrectDoNotRecommendLocation { span: attr_span });
false
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl.
fn check_do_not_recommend(&self, attr_span: Span, hir_id: HirId, target: Target) -> bool {
if !matches!(target, Target::Impl) {
self.tcx.emit_node_span_lint(
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
hir_id,
attr_span,
errors::IncorrectDoNotRecommendLocation,
);
}
true
}

/// Checks if `#[diagnostic::on_unimplemented]` is applied to a trait definition
fn check_diagnostic_on_unimplemented(&self, attr_span: Span, hir_id: HirId, target: Target) {
fn check_diagnostic_on_unimplemented(
&self,
attr_span: Span,
hir_id: HirId,
target: Target,
) -> bool {
if !matches!(target, Target::Trait) {
self.tcx.emit_node_span_lint(
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
Expand All @@ -313,6 +325,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
DiagnosticOnUnimplementedOnlyForTraits,
);
}
true
}

/// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ use rustc_span::{Span, Symbol, DUMMY_SP};
use crate::check_attr::ProcMacroKind;
use crate::lang_items::Duplicate;

#[derive(Diagnostic)]
#[derive(LintDiagnostic)]
#[diag(passes_incorrect_do_not_recommend_location)]
pub struct IncorrectDoNotRecommendLocation {
#[primary_span]
pub span: Span,
}
pub struct IncorrectDoNotRecommendLocation;

#[derive(LintDiagnostic)]
#[diag(passes_outer_crate_level_attr)]
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if res == Res::NonMacroAttr(NonMacroAttrKind::Tool)
&& let [namespace, attribute, ..] = &*path.segments
&& namespace.ident.name == sym::diagnostic
&& attribute.ident.name != sym::on_unimplemented
&& !(attribute.ident.name == sym::on_unimplemented
|| (attribute.ident.name == sym::do_not_recommend
&& self.tcx.features().do_not_recommend))
{
let distance =
edit_distance(attribute.ident.name.as_str(), sym::on_unimplemented.as_str(), 5);
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_trait_selection/src/solve/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,12 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
source: CandidateSource::Impl(impl_def_id),
result: _,
} = candidate.kind()
&& goal.infcx().tcx.has_attr(impl_def_id, sym::do_not_recommend)
&& goal
.infcx()
.tcx
.get_attrs_by_path(impl_def_id, &[sym::diagnostic, sym::do_not_recommend])
.next()
.is_some()
{
return ControlFlow::Break(self.obligation.clone());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let mut base_cause = obligation.cause.code().clone();
loop {
if let ObligationCauseCode::ImplDerived(ref c) = base_cause {
if self.tcx.has_attr(c.impl_or_alias_def_id, sym::do_not_recommend) {
if self
.tcx
.get_attrs_by_path(
c.impl_or_alias_def_id,
&[sym::diagnostic, sym::do_not_recommend],
)
.next()
.is_some()
{
let code = (*c.derived.parent_code).clone();
obligation.cause.map_code(|_| code);
obligation.predicate = c.derived.parent_trait_pred.upcast(self.tcx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<T> Expression for Bound<T> {
type SqlType = T;
}

#[do_not_recommend]
#[diagnostic::do_not_recommend]
impl<T, ST> AsExpression<ST> for T
where
T: Expression<SqlType = ST>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#![feature(do_not_recommend)]

pub trait Foo {
}
pub trait Foo {}

impl Foo for i32 {
}
impl Foo for i32 {}

pub trait Bar {
}
pub trait Bar {}

#[do_not_recommend]
impl<T: Foo> Bar for T {
}
#[diagnostic::do_not_recommend]
impl<T: Foo> Bar for T {}

fn stuff<T: Bar>(_: T) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0277]: the trait bound `u8: Bar` is not satisfied
--> $DIR/feature-gate-do_not_recommend.rs:19:11
--> $DIR/feature-gate-do_not_recommend.rs:15:11
|
LL | stuff(1u8);
| ^^^ the trait `Bar` is not implemented for `u8`
|
note: required by a bound in `stuff`
--> $DIR/feature-gate-do_not_recommend.rs:16:13
--> $DIR/feature-gate-do_not_recommend.rs:12:13
|
LL | fn stuff<T: Bar>(_: T) {}
| ^^^ required by this bound in `stuff`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//@ check-pass
#![feature(do_not_recommend)]

#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
const CONST: () = ();

#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
static STATIC: () = ();

#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
type Type = ();

#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
enum Enum {}

#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
extern "C" {}

#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
fn fun() {}

#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
struct Struct {}

#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
trait Trait {}

#[diagnostic::do_not_recommend]
impl Trait for i32 {}

fn main() {}

0 comments on commit 347462c

Please sign in to comment.