1
Fork 0

entirely remove rustc_args_required_const attribute

This commit is contained in:
Ralf Jung 2021-05-09 14:21:33 +02:00
parent 22e1778ec0
commit 44a8e8d745
14 changed files with 4 additions and 254 deletions

View file

@ -14,7 +14,6 @@ use rustc_middle::ty::cast::{CastTy, IntTy};
use rustc_middle::ty::layout::HasTyCtxt;
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, Ty, TyCtxt};
use rustc_span::source_map::{Span, DUMMY_SP};
use rustc_span::symbol::sym;
use rustc_target::abi::{Abi, Int, LayoutOf, Variants};
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
@ -187,9 +186,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
mir::CastKind::Pointer(PointerCast::ReifyFnPointer) => {
match *operand.layout.ty.kind() {
ty::FnDef(def_id, substs) => {
if bx.cx().tcx().has_attr(def_id, sym::rustc_args_required_const) {
bug!("reifying a fn ptr that requires const arguments");
}
let instance = ty::Instance::resolve_for_fn_ptr(
bx.tcx(),
ty::ParamEnv::reveal_all(),

View file

@ -469,7 +469,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
// ==========================================================================
rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
rustc_attr!(rustc_args_required_const, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
rustc_attr!(rustc_legacy_const_generics, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
// ==========================================================================

View file

@ -7,7 +7,6 @@ use rustc_middle::mir::CastKind;
use rustc_middle::ty::adjustment::PointerCast;
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
use rustc_middle::ty::{self, FloatTy, Ty, TypeAndMut};
use rustc_span::symbol::sym;
use rustc_target::abi::{Integer, LayoutOf, Variants};
use super::{
@ -49,13 +48,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
// All reifications must be monomorphic, bail out otherwise.
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
if self.tcx.has_attr(def_id, sym::rustc_args_required_const) {
span_bug!(
self.cur_span(),
"reifying a fn ptr that requires const arguments"
);
}
let instance = ty::Instance::resolve_for_fn_ptr(
*self.tcx,
self.param_env,

View file

@ -305,7 +305,6 @@ where
let base_intern_mode = match intern_kind {
InternKind::Static(mutbl) => InternMode::Static(mutbl),
// `Constant` includes array lengths.
// `Promoted` includes non-`Copy` array initializers and `rustc_args_required_const` arguments.
InternKind::Constant | InternKind::Promoted => InternMode::Const,
};

View file

@ -32,8 +32,8 @@ use crate::transform::MirPass;
/// A `MirPass` for promotion.
///
/// Promotion is the extraction of promotable temps into separate MIR bodies. This pass also emits
/// errors when promotion of `#[rustc_args_required_const]` arguments fails.
/// Promotion is the extraction of promotable temps into separate MIR bodies so they can have
/// `'static` lifetime.
///
/// After this pass is run, `promoted_fragments` will hold the MIR body corresponding to each
/// newly created `Constant`.

View file

@ -13,9 +13,7 @@ use rustc_errors::{pluralize, struct_span_err, Applicability};
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{
self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID,
};
use rustc_hir::{self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID};
use rustc_hir::{MethodKind, Target};
use rustc_session::lint::builtin::{
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, UNUSED_ATTRIBUTES,
@ -81,9 +79,6 @@ impl CheckAttrVisitor<'tcx> {
sym::doc => self.check_doc_attrs(attr, hir_id, target, &mut specified_inline),
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_args_required_const => {
self.check_rustc_args_required_const(&attr, span, target, item)
}
sym::rustc_layout_scalar_valid_range_start
| sym::rustc_layout_scalar_valid_range_end => {
self.check_rustc_layout_scalar_valid_range(&attr, span, target)
@ -948,79 +943,6 @@ impl CheckAttrVisitor<'tcx> {
}
}
/// Checks if `#[rustc_args_required_const]` is applied to a function and has a valid argument.
fn check_rustc_args_required_const(
&self,
attr: &Attribute,
span: &Span,
target: Target,
item: Option<ItemLike<'_>>,
) -> bool {
let is_function = matches!(target, Target::Fn | Target::Method(..) | Target::ForeignFn);
if !is_function {
self.tcx
.sess
.struct_span_err(attr.span, "attribute should be applied to a function")
.span_label(*span, "not a function")
.emit();
return false;
}
let list = match attr.meta_item_list() {
// The attribute form is validated on AST.
None => return false,
Some(it) => it,
};
let mut invalid_args = vec![];
for meta in list {
if let Some(LitKind::Int(val, _)) = meta.literal().map(|lit| &lit.kind) {
if let Some(ItemLike::Item(Item {
kind: ItemKind::Fn(FnSig { decl, .. }, ..),
..
}))
| Some(ItemLike::ForeignItem(ForeignItem {
kind: ForeignItemKind::Fn(decl, ..),
..
})) = item
{
let arg_count = decl.inputs.len() as u128;
if *val >= arg_count {
let span = meta.span();
self.tcx
.sess
.struct_span_err(span, "index exceeds number of arguments")
.span_label(
span,
format!(
"there {} only {} argument{}",
if arg_count != 1 { "are" } else { "is" },
arg_count,
pluralize!(arg_count)
),
)
.emit();
return false;
}
} else {
bug!("should be a function item");
}
} else {
invalid_args.push(meta.span());
}
}
if !invalid_args.is_empty() {
self.tcx
.sess
.struct_span_err(invalid_args, "arguments should be non-negative integers")
.emit();
false
} else {
true
}
}
fn check_rustc_layout_scalar_valid_range(
&self,
attr: &Attribute,

View file

@ -992,7 +992,6 @@ symbols! {
rustc_allocator,
rustc_allocator_nounwind,
rustc_allow_const_fn_unstable,
rustc_args_required_const,
rustc_attrs,
rustc_builtin_macro,
rustc_capture_analysis,

View file

@ -1537,8 +1537,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
self.check_rustc_args_require_const(def_id, hir_id, span);
debug!("instantiate_value_path: type of {:?} is {:?}", hir_id, ty_substituted);
self.write_substs(hir_id, substs);

View file

@ -18,7 +18,7 @@ use rustc_middle::ty::adjustment::AllowTwoPhase;
use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::{self, Ty};
use rustc_session::Session;
use rustc_span::symbol::{sym, Ident};
use rustc_span::symbol::Ident;
use rustc_span::{self, MultiSpan, Span};
use rustc_trait_selection::traits::{self, ObligationCauseCode, StatementAsExpression};
@ -720,34 +720,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty
}
pub(in super::super) fn check_rustc_args_require_const(
&self,
def_id: DefId,
hir_id: hir::HirId,
span: Span,
) {
// We're only interested in functions tagged with
// #[rustc_args_required_const], so ignore anything that's not.
if !self.tcx.has_attr(def_id, sym::rustc_args_required_const) {
return;
}
// If our calling expression is indeed the function itself, we're good!
// If not, generate an error that this can only be called directly.
if let Node::Expr(expr) = self.tcx.hir().get(self.tcx.hir().get_parent_node(hir_id)) {
if let ExprKind::Call(ref callee, ..) = expr.kind {
if callee.hir_id == hir_id {
return;
}
}
}
self.tcx.sess.span_err(
span,
"this function can only be invoked directly, not through a function pointer",
);
}
/// A common error is to add an extra semicolon:
///
/// ```

View file

@ -1,17 +0,0 @@
// check-pass
#![feature(rustc_attrs)]
#[rustc_args_required_const(0)]
pub const fn a(value: u8) -> u8 {
value
}
#[rustc_args_required_const(0)]
pub fn b(_: u8) {
unimplemented!()
}
fn main() {
let _ = b(a(0));
}

View file

@ -1,32 +0,0 @@
#![feature(rustc_attrs)]
#[rustc_args_required_const(0)] //~ ERROR index exceeds number of arguments
fn foo1() {}
#[rustc_args_required_const(1)] //~ ERROR index exceeds number of arguments
fn foo2(_: u8) {}
#[rustc_args_required_const(a)] //~ ERROR arguments should be non-negative integers
fn foo4() {}
#[rustc_args_required_const(1, a, 2, b)] //~ ERROR arguments should be non-negative integers
fn foo5(_: u8, _: u8, _: u8) {}
#[rustc_args_required_const(0)] //~ ERROR attribute should be applied to a function
struct S;
#[rustc_args_required_const(0usize)] //~ ERROR suffixed literals are not allowed in attributes
fn foo6(_: u8) {}
extern {
#[rustc_args_required_const(1)] //~ ERROR index exceeds number of arguments
fn foo7(_: u8);
}
#[rustc_args_required_const] //~ ERROR malformed `rustc_args_required_const` attribute
fn bar1() {}
#[rustc_args_required_const = 1] //~ ERROR malformed `rustc_args_required_const` attribute
fn bar2() {}
fn main() {}

View file

@ -1,60 +0,0 @@
error: suffixed literals are not allowed in attributes
--> $DIR/invalid-rustc_args_required_const-arguments.rs:18:29
|
LL | #[rustc_args_required_const(0usize)]
| ^^^^^^
|
= help: instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), use an unsuffixed version (`1`, `1.0`, etc.)
error: malformed `rustc_args_required_const` attribute input
--> $DIR/invalid-rustc_args_required_const-arguments.rs:26:1
|
LL | #[rustc_args_required_const]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_args_required_const(N)]`
error: malformed `rustc_args_required_const` attribute input
--> $DIR/invalid-rustc_args_required_const-arguments.rs:29:1
|
LL | #[rustc_args_required_const = 1]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_args_required_const(N)]`
error: index exceeds number of arguments
--> $DIR/invalid-rustc_args_required_const-arguments.rs:3:29
|
LL | #[rustc_args_required_const(0)]
| ^ there are only 0 arguments
error: index exceeds number of arguments
--> $DIR/invalid-rustc_args_required_const-arguments.rs:6:29
|
LL | #[rustc_args_required_const(1)]
| ^ there is only 1 argument
error: arguments should be non-negative integers
--> $DIR/invalid-rustc_args_required_const-arguments.rs:9:29
|
LL | #[rustc_args_required_const(a)]
| ^
error: arguments should be non-negative integers
--> $DIR/invalid-rustc_args_required_const-arguments.rs:12:32
|
LL | #[rustc_args_required_const(1, a, 2, b)]
| ^ ^
error: attribute should be applied to a function
--> $DIR/invalid-rustc_args_required_const-arguments.rs:15:1
|
LL | #[rustc_args_required_const(0)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | struct S;
| --------- not a function
error: index exceeds number of arguments
--> $DIR/invalid-rustc_args_required_const-arguments.rs:22:33
|
LL | #[rustc_args_required_const(1)]
| ^ there is only 1 argument
error: aborting due to 9 previous errors

View file

@ -1,10 +0,0 @@
#![feature(rustc_attrs)]
#[rustc_args_required_const(0)]
fn foo(_a: i32) {
}
fn main() {
let a = foo; //~ ERROR: this function can only be invoked directly
a(2);
}

View file

@ -1,8 +0,0 @@
error: this function can only be invoked directly, not through a function pointer
--> $DIR/rustc-args-required-const2.rs:8:13
|
LL | let a = foo;
| ^^^
error: aborting due to previous error