Skip to content

Validate inline attribute arguments #22490

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

Merged
merged 1 commit into from
Feb 23, 2015
Merged
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
12 changes: 2 additions & 10 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
encode_attributes(rbml_w, &ast_method.attrs);
let scheme = ty::lookup_item_type(ecx.tcx, m.def_id);
let any_types = !scheme.generics.types.is_empty();
if any_types || is_default_impl || should_inline(&ast_method.attrs) {
if any_types || is_default_impl || attr::requests_inline(&ast_method.attrs) {
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id),
ast_item_opt.unwrap()));
}
Expand Down Expand Up @@ -954,14 +954,6 @@ const FN_FAMILY: char = 'f';
const STATIC_METHOD_FAMILY: char = 'F';
const METHOD_FAMILY: char = 'h';

fn should_inline(attrs: &[ast::Attribute]) -> bool {
use syntax::attr::*;
match find_inline_attr(attrs) {
InlineNone | InlineNever => false,
InlineHint | InlineAlways => true
}
}

// Encodes the inherent implementations of a structure, enumeration, or trait.
fn encode_inherent_implementations(ecx: &EncodeContext,
rbml_w: &mut Encoder,
Expand Down Expand Up @@ -1067,7 +1059,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_name(rbml_w, item.ident.name);
encode_path(rbml_w, path);
encode_attributes(rbml_w, &item.attrs);
if tps_len > 0 || should_inline(&item.attrs) {
if tps_len > 0 || attr::requests_inline(&item.attrs) {
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
}
if tps_len == 0 {
Expand Down
17 changes: 3 additions & 14 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,10 @@ use syntax::abi;
use syntax::ast;
use syntax::ast_map;
use syntax::ast_util::{is_local, PostExpansionMethod};
use syntax::attr::{InlineAlways, InlineHint, InlineNever, InlineNone};
use syntax::attr;
use syntax::visit::Visitor;
use syntax::visit;

// Returns true if the given set of attributes contains the `#[inline]`
// attribute.
fn attributes_specify_inlining(attrs: &[ast::Attribute]) -> bool {
match attr::find_inline_attr(attrs) {
InlineNone | InlineNever => false,
InlineAlways | InlineHint => true,
}
}

// Returns true if the given set of generics implies that the item it's
// associated with must be inlined.
fn generics_require_inlining(generics: &ast::Generics) -> bool {
Expand All @@ -50,7 +40,7 @@ fn generics_require_inlining(generics: &ast::Generics) -> bool {
// monomorphized or it was marked with `#[inline]`. This will only return
// true for functions.
fn item_might_be_inlined(item: &ast::Item) -> bool {
if attributes_specify_inlining(&item.attrs) {
if attr::requests_inline(&item.attrs) {
return true
}

Expand All @@ -65,7 +55,7 @@ fn item_might_be_inlined(item: &ast::Item) -> bool {

fn method_might_be_inlined(tcx: &ty::ctxt, method: &ast::Method,
impl_src: ast::DefId) -> bool {
if attributes_specify_inlining(&method.attrs) ||
if attr::requests_inline(&method.attrs) ||
generics_require_inlining(method.pe_generics()) {
return true
}
Expand Down Expand Up @@ -201,8 +191,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
match *impl_item {
ast::MethodImplItem(ref method) => {
if generics_require_inlining(method.pe_generics()) ||
attributes_specify_inlining(
&method.attrs) {
attr::requests_inline(&method.attrs) {
true
} else {
let impl_did = self.tcx
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ pub fn set_inline_hint(f: ValueRef) {
pub fn set_llvm_fn_attrs(ccx: &CrateContext, attrs: &[ast::Attribute], llfn: ValueRef) {
use syntax::attr::*;
// Set the inline hint if there is one
match find_inline_attr(attrs) {
match find_inline_attr(Some(ccx.sess().diagnostic()), attrs) {
InlineHint => set_inline_hint(llfn),
InlineAlways => set_always_inline(llfn),
InlineNever => set_no_inline(llfn),
Expand Down
12 changes: 8 additions & 4 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub enum InlineAttr {
}

/// Determine what `#[inline]` attribute is present in `attrs`, if any.
pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr {
pub fn find_inline_attr(diagnostic: Option<&SpanHandler>, attrs: &[Attribute]) -> InlineAttr {
// FIXME (#2809)---validate the usage of #[inline] and #[inline]
attrs.iter().fold(InlineNone, |ia,attr| {
match attr.node.value.node {
Expand All @@ -302,12 +302,16 @@ pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr {
}
MetaList(ref n, ref items) if *n == "inline" => {
mark_used(attr);
if contains_name(&items[..], "always") {
if items.len() != 1 {
diagnostic.map(|d|{ d.span_err(attr.span, "expected one argument"); });
InlineNone
} else if contains_name(&items[..], "always") {
InlineAlways
} else if contains_name(&items[..], "never") {
InlineNever
} else {
InlineHint
diagnostic.map(|d|{ d.span_err((*items[0]).span, "invalid argument"); });
InlineNone
}
}
_ => ia
Expand All @@ -317,7 +321,7 @@ pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr {

/// True if `#[inline]` or `#[inline(always)]` is present in `attrs`.
pub fn requests_inline(attrs: &[Attribute]) -> bool {
match find_inline_attr(attrs) {
match find_inline_attr(None, attrs) {
InlineHint | InlineAlways => true,
InlineNone | InlineNever => false,
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/compile-fail/invalid-inline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(dead_code)]

#[inline(please_no)] //~ ERROR invalid argument
fn a() {
}

#[inline(please,no)] //~ ERROR expected one argument
fn b() {
}

#[inline()] //~ ERROR expected one argument
fn c() {
}

fn main() {}