-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add -Z teach
flag to provide extended diagnostic help
#47652
Changes from 9 commits
9adf2b2
4e68e2a
fdfb9a2
e76d3f6
864f6d1
ffb827a
7d41cba
3dac0f5
482f7f1
2b73733
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,6 +244,7 @@ pub struct Handler { | |
continue_after_error: Cell<bool>, | ||
delayed_span_bug: RefCell<Option<Diagnostic>>, | ||
tracked_diagnostics: RefCell<Option<Vec<Diagnostic>>>, | ||
tracked_diagnostic_codes: RefCell<FxHashSet<DiagnosticId>>, | ||
|
||
// This set contains a hash of every diagnostic that has been emitted by | ||
// this handler. These hashes is used to avoid emitting the same error | ||
|
@@ -303,6 +304,7 @@ impl Handler { | |
continue_after_error: Cell::new(true), | ||
delayed_span_bug: RefCell::new(None), | ||
tracked_diagnostics: RefCell::new(None), | ||
tracked_diagnostic_codes: RefCell::new(FxHashSet()), | ||
emitted_diagnostics: RefCell::new(FxHashSet()), | ||
} | ||
} | ||
|
@@ -575,13 +577,21 @@ impl Handler { | |
(ret, diagnostics) | ||
} | ||
|
||
pub fn code_emitted(&self, code: &DiagnosticId) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment (short one would suffice) e.g., |
||
self.tracked_diagnostic_codes.borrow().contains(code) | ||
} | ||
|
||
fn emit_db(&self, db: &DiagnosticBuilder) { | ||
let diagnostic = &**db; | ||
|
||
if let Some(ref mut list) = *self.tracked_diagnostics.borrow_mut() { | ||
list.push(diagnostic.clone()); | ||
} | ||
|
||
if let Some(ref code) = diagnostic.code { | ||
self.tracked_diagnostic_codes.borrow_mut().insert(code.clone()); | ||
} | ||
|
||
let diagnostic_hash = { | ||
use std::hash::Hash; | ||
let mut hasher = StableHasher::new(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,10 +281,12 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { | |
.emit(); | ||
} | ||
CastError::SizedUnsizedCast => { | ||
type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0607, | ||
"cannot cast thin pointer `{}` to fat pointer `{}`", | ||
self.expr_ty, | ||
fcx.ty_to_string(self.cast_ty)).emit(); | ||
use structured_errors::{SizedUnsizedCastError, StructuredDiagnostic}; | ||
SizedUnsizedCastError::new(&fcx.tcx.sess, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I'd rather something with labels I think, like:
These labels would then also appear in the templating logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking that the templating could look something like the following:
In the background it would convert the template string into calls to How does that sound? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting. Quite different than what I had in mind, but I like it as a start, and maybe more practical than some of my more grandiose ideas =) I think one key question is whether the I think I had initially imagined that, in
For more complex examples, there might be multiple snippets:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason I wasn't considering moving in that direction is because I though we wouldn't want to present the same explanation over and over again, so mixing the current diagnostics with the teach diagnostics would be a bit jarring. But if we don't mind either mixing both styles or repeating walls of text, then I don't see why we wouldn't do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think that the differences in output could act like a stylesheet, where the |
||
self.span, | ||
self.expr_ty, | ||
fcx.ty_to_string(self.cast_ty)) | ||
.diagnostic().emit(); | ||
} | ||
CastError::UnknownCastPtrKind | | ||
CastError::UnknownExprPtrKind => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright 2018 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. | ||
|
||
use rustc::session::Session; | ||
use syntax_pos::Span; | ||
use errors::{DiagnosticId, DiagnosticBuilder}; | ||
use rustc::ty::{Ty, TypeFoldable}; | ||
|
||
pub trait StructuredDiagnostic<'tcx> { | ||
fn session(&self) -> &Session; | ||
|
||
fn code(&self) -> DiagnosticId; | ||
|
||
fn common(&self) -> DiagnosticBuilder<'tcx>; | ||
|
||
fn diagnostic(&self) -> DiagnosticBuilder<'tcx> { | ||
let err = self.common(); | ||
if self.session().teach(&self.code()) { | ||
self.extended(err) | ||
} else { | ||
self.regular(err) | ||
} | ||
} | ||
|
||
fn regular(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> { | ||
err | ||
} | ||
|
||
fn extended(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> { | ||
err | ||
} | ||
} | ||
|
||
pub struct VariadicError<'tcx> { | ||
sess: &'tcx Session, | ||
span: Span, | ||
t: Ty<'tcx>, | ||
cast_ty: &'tcx str, | ||
} | ||
|
||
impl<'tcx> VariadicError<'tcx> { | ||
pub fn new(sess: &'tcx Session, | ||
span: Span, | ||
t: Ty<'tcx>, | ||
cast_ty: &'tcx str) -> VariadicError<'tcx> { | ||
VariadicError { sess, span, t, cast_ty } | ||
} | ||
} | ||
|
||
impl<'tcx> StructuredDiagnostic<'tcx> for VariadicError<'tcx> { | ||
fn session(&self) -> &Session { self.sess } | ||
|
||
fn code(&self) -> DiagnosticId { | ||
__diagnostic_used!(E0617); | ||
DiagnosticId::Error("E0617".to_owned()) | ||
} | ||
|
||
fn common(&self) -> DiagnosticBuilder<'tcx> { | ||
let mut err = if self.t.references_error() { | ||
self.sess.diagnostic().struct_dummy() | ||
} else { | ||
self.sess.struct_span_fatal_with_code( | ||
self.span, | ||
&format!("can't pass `{}` to variadic function", self.t), | ||
self.code(), | ||
) | ||
}; | ||
if let Ok(snippet) = self.sess.codemap().span_to_snippet(self.span) { | ||
err.span_suggestion(self.span, | ||
&format!("cast the value to `{}`", self.cast_ty), | ||
format!("{} as {}", snippet, self.cast_ty)); | ||
} else { | ||
err.help(&format!("cast the value to `{}`", self.cast_ty)); | ||
} | ||
err | ||
} | ||
|
||
fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> { | ||
err.note(&format!("certain types, like `{}`, must be cast before passing them to a \ | ||
variadic function, because of arcane ABI rules dictated by the C \ | ||
standard", | ||
self.t)); | ||
err | ||
} | ||
} | ||
|
||
pub struct SizedUnsizedCastError<'tcx> { | ||
sess: &'tcx Session, | ||
span: Span, | ||
expr_ty: Ty<'tcx>, | ||
cast_ty: String, | ||
} | ||
|
||
impl<'tcx> SizedUnsizedCastError<'tcx> { | ||
pub fn new(sess: &'tcx Session, | ||
span: Span, | ||
expr_ty: Ty<'tcx>, | ||
cast_ty: String) -> SizedUnsizedCastError<'tcx> { | ||
SizedUnsizedCastError { sess, span, expr_ty, cast_ty } | ||
} | ||
} | ||
|
||
impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCastError<'tcx> { | ||
fn session(&self) -> &Session { self.sess } | ||
|
||
fn code(&self) -> DiagnosticId { | ||
__diagnostic_used!(E0607); | ||
DiagnosticId::Error("E0607".to_owned()) | ||
} | ||
|
||
fn common(&self) -> DiagnosticBuilder<'tcx> { | ||
if self.expr_ty.references_error() { | ||
self.sess.diagnostic().struct_dummy() | ||
} else { | ||
self.sess.struct_span_fatal_with_code( | ||
self.span, | ||
&format!("cannot cast thin pointer `{}` to fat pointer `{}`", | ||
self.expr_ty, | ||
self.cast_ty), | ||
self.code(), | ||
) | ||
} | ||
} | ||
|
||
fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> { | ||
err.help( | ||
"Thin pointers are \"simple\" pointers: they are purely a reference to a | ||
memory address. | ||
|
||
Fat pointers are pointers referencing \"Dynamically Sized Types\" (also | ||
called DST). DST don't have a statically known size, therefore they can | ||
only exist behind some kind of pointers that contain additional | ||
information. Slices and trait objects are DSTs. In the case of slices, | ||
the additional information the fat pointer holds is their size. | ||
|
||
To fix this error, don't try to cast directly between thin and fat | ||
pointers. | ||
|
||
For more information about casts, take a look at The Book: | ||
https://doc.rust-lang.org/book/first-edition/casting-between-types.html"); | ||
err | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we put a comment on this field explaining what it's for?