Skip to content

Commit 5b9fc63

Browse files
committed
---
yaml --- r: 172890 b: refs/heads/batch c: dc0de42 h: refs/heads/master v: v3
1 parent 3a431f7 commit 5b9fc63

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
2929
refs/heads/issue-18208-method-dispatch-2: 9e1eae4fb9b6527315b4441cf8a0f5ca911d1671
3030
refs/heads/automation-fail: 1bf06495443584539b958873e04cc2f864ab10e4
3131
refs/heads/issue-18208-method-dispatch-3-quick-reject: 2009f85b9f99dedcec4404418eda9ddba90258a2
32-
refs/heads/batch: 4d17fbaf37a0641894317f016244943af66ce87b
32+
refs/heads/batch: dc0de42035dc5c71e47ded2968fc6fd7c76641c6
3333
refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
3434
refs/heads/beta: 44a287e6eb22ec3c2a687fc156813577464017f7
3535
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928

branches/batch/src/librustc/lint/builtin.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
//! a `pub fn new()`.
2727
use self::MethodContext::*;
2828

29+
30+
use fmt_macros::{Parser, Piece, Position};
31+
2932
use metadata::csearch;
3033
use middle::def::*;
3134
use middle::subst::Substs;
@@ -1921,3 +1924,63 @@ impl LintPass for UnstableFeatures {
19211924
}
19221925
}
19231926
}
1927+
1928+
/// Checks usage of `#[on_unimplemented]`
1929+
#[derive(Copy)]
1930+
pub struct BadOnUnimplemented;
1931+
1932+
declare_lint!(BAD_ON_UNIMPLEMENTED, Deny,
1933+
"Checks usage of `#[on_unimplemented]`");
1934+
1935+
impl LintPass for BadOnUnimplemented {
1936+
fn get_lints(&self) -> LintArray {
1937+
lint_array!(BAD_ON_UNIMPLEMENTED)
1938+
}
1939+
fn check_item(&mut self, ctx: &Context, item: &ast::Item) {
1940+
match item.node {
1941+
ast::ItemTrait(_, ref generics, _, _) => {
1942+
if let Some(ref attr) = item.attrs.iter().find(|&: a| {
1943+
a.check_name("on_unimplemented")
1944+
}) {
1945+
if let Some(ref istring) = attr.value_str() {
1946+
let mut parser = Parser::new(istring.get());
1947+
let types = generics.ty_params.as_slice();
1948+
for token in parser {
1949+
match token {
1950+
Piece::String(_) => (), // Normal string, no need to check it
1951+
Piece::NextArgument(a) => match a.position {
1952+
// `{Self}` is allowed
1953+
Position::ArgumentNamed(s) if s == "Self" => (),
1954+
// So is `{A}` if A is a type parameter
1955+
Position::ArgumentNamed(s) => match types.iter().find(|t| {
1956+
t.ident.as_str() == s
1957+
}) {
1958+
Some(_) => (),
1959+
None => {
1960+
ctx.span_lint(BAD_ON_UNIMPLEMENTED, attr.span,
1961+
format!("there is no type parameter \
1962+
{} on trait {}",
1963+
s, item.ident.as_str())
1964+
.as_slice());
1965+
}
1966+
},
1967+
// `{:1}` and `{}` are not to be used
1968+
Position::ArgumentIs(_) | Position::ArgumentNext => {
1969+
ctx.span_lint(BAD_ON_UNIMPLEMENTED, attr.span,
1970+
"only named substitution \
1971+
parameters are allowed");
1972+
}
1973+
}
1974+
}
1975+
}
1976+
} else {
1977+
ctx.span_lint(BAD_ON_UNIMPLEMENTED, attr.span,
1978+
"this attribute must have a value, \
1979+
eg `#[on_unimplemented = \"foo\"]`")
1980+
}
1981+
}
1982+
},
1983+
_ => () // Not a trait def, move along
1984+
}
1985+
}
1986+
}

branches/batch/src/librustc/lint/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ impl LintStore {
211211
UnusedAllocation,
212212
MissingCopyImplementations,
213213
UnstableFeatures,
214+
BadOnUnimplemented,
214215
);
215216

216217
add_builtin_with_new!(sess,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
// ignore-tidy-linelength
11+
12+
#[allow(unused)]
13+
14+
#[on_unimplemented = "test error `{Self}` with `{Bar}` `{Baz}` `{Quux}`"]
15+
trait Foo<Bar, Baz, Quux>{}
16+
17+
#[on_unimplemented="a collection of type `{Self}` cannot be built from an iterator over elements of type `{A}`"]
18+
trait MyFromIterator<A> {
19+
/// Build a container with elements from an external iterator.
20+
fn my_from_iter<T: Iterator<Item=A>>(iterator: T) -> Self;
21+
}
22+
23+
#[on_unimplemented] //~ ERROR this attribute must have a value
24+
trait BadAnnotation1 {}
25+
26+
#[on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{C}>`"]
27+
//~^ ERROR there is no type parameter C on trait BadAnnotation2
28+
trait BadAnnotation2<A,B> {}
29+
30+
31+
pub fn main() {
32+
}

0 commit comments

Comments
 (0)