1
Fork 0

Add error codes for errors in libsyntax

This commit is contained in:
Guillaume Gomez 2017-02-12 14:33:17 +01:00
parent 0128be9ad7
commit ea2a684099
2 changed files with 86 additions and 40 deletions

View file

@ -224,4 +224,8 @@ register_diagnostics! {
E0555, // malformed feature attribute, expected #![feature(...)] E0555, // malformed feature attribute, expected #![feature(...)]
E0556, // malformed feature, expected just one word E0556, // malformed feature, expected just one word
E0557, // feature has been removed E0557, // feature has been removed
E0583, // file not found for module
E0584, // file for module `..` found at both .. and ..
E0585, // documentation comment that doesn't document anything
E0586, // inclusive range with no end
} }

View file

@ -219,7 +219,7 @@ fn is_ident_or_underscore(t: &token::Token) -> bool {
pub struct ModulePath { pub struct ModulePath {
pub name: String, pub name: String,
pub path_exists: bool, pub path_exists: bool,
pub result: Result<ModulePathSuccess, ModulePathError>, pub result: Result<ModulePathSuccess, Errors>,
} }
pub struct ModulePathSuccess { pub struct ModulePathSuccess {
@ -233,6 +233,63 @@ pub struct ModulePathError {
pub help_msg: String, pub help_msg: String,
} }
pub enum Errors {
FileNotFoundForModule {
mod_name: String,
default_path: String,
secondary_path: String,
dir_path: String,
},
DuplicatePaths {
mod_name: String,
default_path: String,
secondary_path: String,
},
UselessDocComment,
InclusiveRangeWithNoEnd,
}
impl Errors {
pub fn span_err<'a>(self, sp: Span, handler: &'a errors::Handler) -> DiagnosticBuilder<'a> {
match self {
Errors::FileNotFoundForModule { ref mod_name,
ref default_path,
ref secondary_path,
ref dir_path } => {
let mut err = struct_span_err!(handler, sp, E0583,
"file not found for module `{}`", mod_name);
err.help(&format!("name the file either {} or {} inside the directory {:?}",
default_path,
secondary_path,
dir_path));
err
}
Errors::DuplicatePaths { ref mod_name, ref default_path, ref secondary_path } => {
let mut err = struct_span_err!(handler, sp, E0584,
"file for module `{}` found at both {} and {}",
mod_name,
default_path,
secondary_path);
err.help("delete or rename one of them to remove the ambiguity");
err
}
Errors::UselessDocComment => {
let mut err = struct_span_err!(handler, sp, E0585,
"found a documentation comment that doesn't document anything");
err.help("doc comments must come before what they document, maybe a comment was \
intended with `//`?");
err
}
Errors::InclusiveRangeWithNoEnd => {
let mut err = struct_span_err!(handler, sp, E0586,
"inclusive range with no end");
err.help("inclusive ranges must be bounded at the end (`...b` or `a...b`)");
err
}
}
}
}
pub enum LhsExpr { pub enum LhsExpr {
NotYetParsed, NotYetParsed,
AttributesParsed(ThinVec<Attribute>), AttributesParsed(ThinVec<Attribute>),
@ -461,10 +518,7 @@ impl<'a> Parser<'a> {
} }
_ => { _ => {
Err(if self.prev_token_kind == PrevTokenKind::DocComment { Err(if self.prev_token_kind == PrevTokenKind::DocComment {
self.span_fatal_help(self.prev_span, self.span_fatal_err(self.prev_span, Errors::UselessDocComment)
"found a documentation comment that doesn't document anything",
"doc comments must come before what they document, maybe a comment was \
intended with `//`?")
} else { } else {
let mut err = self.fatal(&format!("expected identifier, found `{}`", let mut err = self.fatal(&format!("expected identifier, found `{}`",
self.this_token_to_string())); self.this_token_to_string()));
@ -955,6 +1009,9 @@ impl<'a> Parser<'a> {
pub fn span_fatal(&self, sp: Span, m: &str) -> DiagnosticBuilder<'a> { pub fn span_fatal(&self, sp: Span, m: &str) -> DiagnosticBuilder<'a> {
self.sess.span_diagnostic.struct_span_fatal(sp, m) self.sess.span_diagnostic.struct_span_fatal(sp, m)
} }
pub fn span_fatal_err(&self, sp: Span, err: Errors) -> DiagnosticBuilder<'a> {
err.span_err(sp, self.diagnostic())
}
pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> DiagnosticBuilder<'a> { pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> DiagnosticBuilder<'a> {
let mut err = self.sess.span_diagnostic.struct_span_fatal(sp, m); let mut err = self.sess.span_diagnostic.struct_span_fatal(sp, m);
err.help(help); err.help(help);
@ -1944,10 +2001,7 @@ impl<'a> Parser<'a> {
limits: RangeLimits) limits: RangeLimits)
-> PResult<'a, ast::ExprKind> { -> PResult<'a, ast::ExprKind> {
if end.is_none() && limits == RangeLimits::Closed { if end.is_none() && limits == RangeLimits::Closed {
Err(self.span_fatal_help(self.span, Err(self.span_fatal_err(self.span, Errors::InclusiveRangeWithNoEnd))
"inclusive range with no end",
"inclusive ranges must be bounded at the end \
(`...b` or `a...b`)"))
} else { } else {
Ok(ExprKind::Range(start, end, limits)) Ok(ExprKind::Range(start, end, limits))
} }
@ -3862,10 +3916,7 @@ impl<'a> Parser<'a> {
let unused_attrs = |attrs: &[_], s: &mut Self| { let unused_attrs = |attrs: &[_], s: &mut Self| {
if attrs.len() > 0 { if attrs.len() > 0 {
if s.prev_token_kind == PrevTokenKind::DocComment { if s.prev_token_kind == PrevTokenKind::DocComment {
s.span_err_help(s.prev_span, self.span_fatal_err(s.prev_span, Errors::UselessDocComment).emit();
"found a documentation comment that doesn't document anything",
"doc comments must come before what they document, maybe a \
comment was intended with `//`?");
} else { } else {
s.span_err(s.span, "expected statement after outer attribute"); s.span_err(s.span, "expected statement after outer attribute");
} }
@ -4998,10 +5049,8 @@ impl<'a> Parser<'a> {
self.bump(); self.bump();
} }
token::CloseDelim(token::Brace) => {} token::CloseDelim(token::Brace) => {}
token::DocComment(_) => return Err(self.span_fatal_help(self.span, token::DocComment(_) => return Err(self.span_fatal_err(self.span,
"found a documentation comment that doesn't document anything", Errors::UselessDocComment)),
"doc comments must come before what they document, maybe a comment was \
intended with `//`?")),
_ => return Err(self.span_fatal_help(self.span, _ => return Err(self.span_fatal_help(self.span,
&format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()), &format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
"struct fields should be separated by commas")), "struct fields should be separated by commas")),
@ -5162,8 +5211,7 @@ impl<'a> Parser<'a> {
} }
/// Returns either a path to a module, or . /// Returns either a path to a module, or .
pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath {
{
let mod_name = id.to_string(); let mod_name = id.to_string();
let default_path_str = format!("{}.rs", mod_name); let default_path_str = format!("{}.rs", mod_name);
let secondary_path_str = format!("{}/mod.rs", mod_name); let secondary_path_str = format!("{}/mod.rs", mod_name);
@ -5183,19 +5231,16 @@ impl<'a> Parser<'a> {
directory_ownership: DirectoryOwnership::Owned, directory_ownership: DirectoryOwnership::Owned,
warn: false, warn: false,
}), }),
(false, false) => Err(ModulePathError { (false, false) => Err(Errors::FileNotFoundForModule {
err_msg: format!("file not found for module `{}`", mod_name), mod_name: mod_name.clone(),
help_msg: format!("name the file either {} or {} inside the directory {:?}", default_path: default_path_str,
default_path_str, secondary_path: secondary_path_str,
secondary_path_str, dir_path: format!("{}", dir_path.display()),
dir_path.display()),
}), }),
(true, true) => Err(ModulePathError { (true, true) => Err(Errors::DuplicatePaths {
err_msg: format!("file for module `{}` found at both {} and {}", mod_name: mod_name.clone(),
mod_name, default_path: default_path_str,
default_path_str, secondary_path: secondary_path_str,
secondary_path_str),
help_msg: "delete or rename one of them to remove the ambiguity".to_owned(),
}), }),
}; };
@ -5232,7 +5277,7 @@ impl<'a> Parser<'a> {
paths.name); paths.name);
err.span_note(id_sp, &msg); err.span_note(id_sp, &msg);
} }
return Err(err); Err(err)
} else if let DirectoryOwnership::UnownedViaMod(warn) = self.directory.ownership { } else if let DirectoryOwnership::UnownedViaMod(warn) = self.directory.ownership {
if warn { if warn {
if let Ok(result) = paths.result { if let Ok(result) = paths.result {
@ -5254,15 +5299,12 @@ impl<'a> Parser<'a> {
&format!("... or maybe `use` the module `{}` instead \ &format!("... or maybe `use` the module `{}` instead \
of possibly redeclaring it", of possibly redeclaring it",
paths.name)); paths.name));
return Err(err); Err(err)
} else { } else {
return Err(err); Err(err)
}; }
} } else {
paths.result.map_err(|err| self.span_fatal_err(id_sp, err))
match paths.result {
Ok(succ) => Ok(succ),
Err(err) => Err(self.span_fatal_help(id_sp, &err.err_msg, &err.help_msg)),
} }
} }