Skip to content

Commit df55cee

Browse files
Rollup merge of rust-lang#47691 - estebank:unknown-lang-item-sp, r=rkruppe
Point at unknown lang item attribute
2 parents bbdc5b9 + e6af9eb commit df55cee

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

src/librustc/middle/lang_items.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use util::nodemap::FxHashMap;
2828

2929
use syntax::ast;
3030
use syntax::symbol::Symbol;
31+
use syntax_pos::Span;
3132
use hir::itemlikevisit::ItemLikeVisitor;
3233
use hir;
3334

@@ -104,17 +105,18 @@ struct LanguageItemCollector<'a, 'tcx: 'a> {
104105

105106
impl<'a, 'v, 'tcx> ItemLikeVisitor<'v> for LanguageItemCollector<'a, 'tcx> {
106107
fn visit_item(&mut self, item: &hir::Item) {
107-
if let Some(value) = extract(&item.attrs) {
108+
if let Some((value, span)) = extract(&item.attrs) {
108109
let item_index = self.item_refs.get(&*value.as_str()).cloned();
109110

110111
if let Some(item_index) = item_index {
111112
let def_id = self.tcx.hir.local_def_id(item.id);
112113
self.collect_item(item_index, def_id);
113114
} else {
114-
let span = self.tcx.hir.span(item.id);
115-
span_err!(self.tcx.sess, span, E0522,
116-
"definition of an unknown language item: `{}`.",
117-
value);
115+
let mut err = struct_span_err!(self.tcx.sess, span, E0522,
116+
"definition of an unknown language item: `{}`",
117+
value);
118+
err.span_label(span, format!("definition of unknown language item `{}`", value));
119+
err.emit();
118120
}
119121
}
120122
}
@@ -177,11 +179,11 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
177179
}
178180
}
179181

180-
pub fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
182+
pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
181183
for attribute in attrs {
182184
if attribute.check_name("lang") {
183185
if let Some(value) = attribute.value_str() {
184-
return Some(value)
186+
return Some((value, attribute.span));
185187
}
186188
}
187189
}

src/librustc/middle/weak_lang_items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
5555
}
5656

5757
pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol> {
58-
lang_items::extract(attrs).and_then(|name| {
58+
lang_items::extract(attrs).and_then(|(name, _)| {
5959
$(if name == stringify!($name) {
6060
Some(Symbol::intern(stringify!($sym)))
6161
} else)* {
@@ -129,7 +129,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
129129
}
130130

131131
fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
132-
if let Some(lang_item) = lang_items::extract(&i.attrs) {
132+
if let Some((lang_item, _)) = lang_items::extract(&i.attrs) {
133133
self.register(&lang_item.as_str(), i.span);
134134
}
135135
intravisit::walk_foreign_item(self, i)

src/test/compile-fail/E0522.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(lang_items)]
1212

1313
#[lang = "cookie"]
14-
fn cookie() -> ! { //~ E0522
14+
fn cookie() -> ! {
15+
//~^^ ERROR definition of an unknown language item: `cookie` [E0522]
1516
loop {}
1617
}

src/test/ui/unknown-language-item.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 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+
11+
#![allow(unused)]
12+
#![feature(lang_items)]
13+
14+
#[lang = "foo"]
15+
fn bar() -> ! {
16+
//~^^ ERROR definition of an unknown language item: `foo`
17+
loop {}
18+
}
19+
20+
fn main() {}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0522]: definition of an unknown language item: `foo`
2+
--> $DIR/unknown-language-item.rs:14:1
3+
|
4+
14 | #[lang = "foo"]
5+
| ^^^^^^^^^^^^^^^ definition of unknown language item `foo`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)