Skip to content
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

Rollup of 6 pull requests #71600

Merged
merged 13 commits into from
Apr 27, 2020
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
2 changes: 1 addition & 1 deletion src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<K, V> BoxedNode<K, V> {
}

unsafe fn from_ptr(ptr: NonNull<LeafNode<K, V>>) -> Self {
BoxedNode { ptr: Unique::from(ptr) }
BoxedNode { ptr: Unique::new_unchecked(ptr.as_ptr()) }
}

fn as_ptr(&self) -> NonNull<LeafNode<K, V>> {
Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<T, A: AllocRef> RawVec<T, A> {

let memory = alloc.alloc(layout, init).unwrap_or_else(|_| handle_alloc_error(layout));
Self {
ptr: memory.ptr.cast().into(),
ptr: unsafe { Unique::new_unchecked(memory.ptr.cast().as_ptr()) },
cap: Self::capacity_from_bytes(memory.size),
alloc,
}
Expand Down Expand Up @@ -469,7 +469,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
}

fn set_memory(&mut self, memory: MemoryBlock) {
self.ptr = memory.ptr.cast().into();
self.ptr = unsafe { Unique::new_unchecked(memory.ptr.cast().as_ptr()) };
self.cap = Self::capacity_from_bytes(memory.size);
}

Expand Down
17 changes: 0 additions & 17 deletions src/libcore/ptr/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::fmt;
use crate::marker::{PhantomData, Unsize};
use crate::mem;
use crate::ops::{CoerceUnsized, DispatchFromDyn};
use crate::ptr::NonNull;

// ignore-tidy-undocumented-unsafe

Expand Down Expand Up @@ -171,19 +170,3 @@ impl<T: ?Sized> From<&mut T> for Unique<T> {
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
}
}

#[unstable(feature = "ptr_internals", issue = "none")]
impl<T: ?Sized> From<&T> for Unique<T> {
#[inline]
fn from(reference: &T) -> Self {
unsafe { Unique { pointer: reference as *const T, _marker: PhantomData } }
}
}

#[unstable(feature = "ptr_internals", issue = "none")]
impl<T: ?Sized> From<NonNull<T>> for Unique<T> {
#[inline]
fn from(p: NonNull<T>) -> Self {
unsafe { Unique::new_unchecked(p.as_ptr()) }
}
}
2 changes: 1 addition & 1 deletion src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl Span {
/// definition site (local variables, labels, `$crate`) and sometimes at the macro
/// call site (everything else).
/// The span location is taken from the call-site.
#[unstable(feature = "proc_macro_mixed_site", issue = "65049")]
#[stable(feature = "proc_macro_mixed_site", since = "1.45.0")]
pub fn mixed_site() -> Span {
Span(bridge::client::Span::mixed_site())
}
Expand Down
39 changes: 24 additions & 15 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
has_self_arg
}

fn followed_by_brace(&self, span: Span) -> (bool, Option<(Span, String)>) {
fn followed_by_brace(&self, span: Span) -> (bool, Option<Span>) {
// HACK(estebank): find a better way to figure out that this was a
// parser issue where a struct literal is being used on an expression
// where a brace being opened means a block is being started. Look
Expand All @@ -406,18 +406,15 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
_ => false,
};
// In case this could be a struct literal that needs to be surrounded
// by parenthesis, find the appropriate span.
// by parentheses, find the appropriate span.
let mut i = 0;
let mut closing_brace = None;
loop {
sp = sm.next_point(sp);
match sm.span_to_snippet(sp) {
Ok(ref snippet) => {
if snippet == "}" {
let sp = span.to(sp);
if let Ok(snippet) = sm.span_to_snippet(sp) {
closing_brace = Some((sp, snippet));
}
closing_brace = Some(span.to(sp));
break;
}
}
Expand Down Expand Up @@ -479,17 +476,23 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
suggested = path_sep(err, &parent);
}
PathSource::Expr(None) if followed_by_brace => {
if let Some((sp, snippet)) = closing_brace {
err.span_suggestion(
sp,
"surround the struct literal with parenthesis",
format!("({})", snippet),
if let Some(sp) = closing_brace {
err.multipart_suggestion(
"surround the struct literal with parentheses",
vec![
(sp.shrink_to_lo(), "(".to_string()),
(sp.shrink_to_hi(), ")".to_string()),
],
Applicability::MaybeIncorrect,
);
} else {
err.span_label(
span, // Note the parenthesis surrounding the suggestion below
format!("did you mean `({} {{ /* fields */ }})`?", path_str),
span, // Note the parentheses surrounding the suggestion below
format!(
"you might want to surround a struct literal with parentheses: \
`({} {{ /* fields */ }})`?",
path_str
),
);
}
suggested = true;
Expand All @@ -516,10 +519,16 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
err.note("if you want the `try` keyword, you need to be in the 2018 edition");
}
}
(Res::Def(DefKind::TyAlias, _), PathSource::Trait(_)) => {
(Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {
err.span_label(span, "type aliases cannot be used as traits");
if nightly_options::is_nightly_build() {
err.note("did you mean to use a trait alias?");
let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
`type` alias";
if let Some(span) = self.r.definitions.opt_span(def_id) {
err.span_help(span, msg);
} else {
err.help(msg);
}
}
}
(Res::Def(DefKind::Mod, _), PathSource::Expr(Some(parent))) => {
Expand Down
128 changes: 83 additions & 45 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2066,52 +2066,64 @@ impl<'a> Resolver<'a> {
};
}

let binding = if let Some(module) = module {
self.resolve_ident_in_module(
module,
ident,
ns,
parent_scope,
record_used,
path_span,
)
} else if ribs.is_none() || opt_ns.is_none() || opt_ns == Some(MacroNS) {
let scopes = ScopeSet::All(ns, opt_ns.is_none());
self.early_resolve_ident_in_lexical_scope(
ident,
scopes,
parent_scope,
record_used,
record_used,
path_span,
)
} else {
let record_used_id =
if record_used { crate_lint.node_id().or(Some(CRATE_NODE_ID)) } else { None };
match self.resolve_ident_in_lexical_scope(
ident,
ns,
parent_scope,
record_used_id,
path_span,
&ribs.unwrap()[ns],
) {
// we found a locally-imported or available item/module
Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
// we found a local variable or type param
Some(LexicalScopeBinding::Res(res))
if opt_ns == Some(TypeNS) || opt_ns == Some(ValueNS) =>
{
record_segment_res(self, res);
return PathResult::NonModule(PartialRes::with_unresolved_segments(
res,
path.len() - 1,
));
enum FindBindingResult<'a> {
Binding(Result<&'a NameBinding<'a>, Determinacy>),
PathResult(PathResult<'a>),
}
let find_binding_in_ns = |this: &mut Self, ns| {
let binding = if let Some(module) = module {
this.resolve_ident_in_module(
module,
ident,
ns,
parent_scope,
record_used,
path_span,
)
} else if ribs.is_none() || opt_ns.is_none() || opt_ns == Some(MacroNS) {
let scopes = ScopeSet::All(ns, opt_ns.is_none());
this.early_resolve_ident_in_lexical_scope(
ident,
scopes,
parent_scope,
record_used,
record_used,
path_span,
)
} else {
let record_used_id = if record_used {
crate_lint.node_id().or(Some(CRATE_NODE_ID))
} else {
None
};
match this.resolve_ident_in_lexical_scope(
ident,
ns,
parent_scope,
record_used_id,
path_span,
&ribs.unwrap()[ns],
) {
// we found a locally-imported or available item/module
Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
// we found a local variable or type param
Some(LexicalScopeBinding::Res(res))
if opt_ns == Some(TypeNS) || opt_ns == Some(ValueNS) =>
{
record_segment_res(this, res);
return FindBindingResult::PathResult(PathResult::NonModule(
PartialRes::with_unresolved_segments(res, path.len() - 1),
));
}
_ => Err(Determinacy::determined(record_used)),
}
_ => Err(Determinacy::determined(record_used)),
}
};
FindBindingResult::Binding(binding)
};
let binding = match find_binding_in_ns(self, ns) {
FindBindingResult::PathResult(x) => return x,
FindBindingResult::Binding(binding) => binding,
};

match binding {
Ok(binding) => {
if i == 1 {
Expand Down Expand Up @@ -2201,7 +2213,33 @@ impl<'a> Resolver<'a> {
} else if i == 0 {
(format!("use of undeclared type or module `{}`", ident), None)
} else {
(format!("could not find `{}` in `{}`", ident, path[i - 1].ident), None)
let mut msg =
format!("could not find `{}` in `{}`", ident, path[i - 1].ident);
if ns == TypeNS || ns == ValueNS {
let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
if let FindBindingResult::Binding(Ok(binding)) =
find_binding_in_ns(self, ns_to_try)
{
let mut found = |what| {
msg = format!(
"expected {}, found {} `{}` in `{}`",
ns.descr(),
what,
ident,
path[i - 1].ident
)
};
if binding.module().is_some() {
found("module")
} else {
match binding.res() {
def::Res::<NodeId>::Def(kind, id) => found(kind.descr(id)),
_ => found(ns_to_try.descr()),
}
}
};
}
(msg, None)
};
return PathResult::Failed {
span: ident.span,
Expand Down
12 changes: 0 additions & 12 deletions src/librustc_span/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering;

use log::debug;
use std::env;
use std::fs;
use std::io;

Expand Down Expand Up @@ -64,9 +63,6 @@ pub trait FileLoader {
/// Query the existence of a file.
fn file_exists(&self, path: &Path) -> bool;

/// Returns an absolute path to a file, if possible.
fn abs_path(&self, path: &Path) -> Option<PathBuf>;

/// Read the contents of an UTF-8 file into memory.
fn read_file(&self, path: &Path) -> io::Result<String>;
}
Expand All @@ -79,14 +75,6 @@ impl FileLoader for RealFileLoader {
fs::metadata(path).is_ok()
}

fn abs_path(&self, path: &Path) -> Option<PathBuf> {
if path.is_absolute() {
Some(path.to_path_buf())
} else {
env::current_dir().ok().map(|cwd| cwd.join(path))
}
}

fn read_file(&self, path: &Path) -> io::Result<String> {
fs::read_to_string(path)
}
Expand Down
38 changes: 24 additions & 14 deletions src/librustc_trait_selection/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,30 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
.starts_with("std::convert::From<std::option::NoneError");
let should_convert_result_to_option = format!("{}", trait_ref)
.starts_with("<std::option::NoneError as std::convert::From<");
if is_try && is_from && should_convert_option_to_result {
err.span_suggestion_verbose(
span.shrink_to_lo(),
"consider converting the `Option<T>` into a `Result<T, _>` using `Option::ok_or` or `Option::ok_or_else`",
".ok_or_else(|| /* error value */)".to_string(),
Applicability::HasPlaceholders,
);
} else if is_try && is_from && should_convert_result_to_option {
err.span_suggestion_verbose(
span.shrink_to_lo(),
"consider converting the `Result<T, _>` into an `Option<T>` using `Result::ok`",
".ok()".to_string(),
Applicability::MachineApplicable,
);
if is_try && is_from {
if should_convert_option_to_result {
err.span_suggestion_verbose(
span.shrink_to_lo(),
"consider converting the `Option<T>` into a `Result<T, _>` \
using `Option::ok_or` or `Option::ok_or_else`",
".ok_or_else(|| /* error value */)".to_string(),
Applicability::HasPlaceholders,
);
} else if should_convert_result_to_option {
err.span_suggestion_verbose(
span.shrink_to_lo(),
"consider converting the `Result<T, _>` into an `Option<T>` \
using `Result::ok`",
".ok()".to_string(),
Applicability::MachineApplicable,
);
}
if let Some(ret_span) = self.return_type_span(obligation) {
err.span_label(
ret_span,
&format!("expected `{}` because of this", trait_ref.self_ty()),
);
}
}

let explanation =
Expand Down
13 changes: 13 additions & 0 deletions src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub trait InferCtxtExt<'tcx> {
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
);

fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span>;

fn suggest_impl_trait(
&self,
err: &mut DiagnosticBuilder<'tcx>,
Expand Down Expand Up @@ -761,6 +763,17 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
}
}

fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
let hir = self.tcx.hir();
let parent_node = hir.get_parent_node(obligation.cause.body_id);
let sig = match hir.find(parent_node) {
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) => sig,
_ => return None,
};

if let hir::FnRetTy::Return(ret_ty) = sig.decl.output { Some(ret_ty.span) } else { None }
}

/// If all conditions are met to identify a returned `dyn Trait`, suggest using `impl Trait` if
/// applicable and signal that the error has been expanded appropriately and needs to be
/// emitted.
Expand Down
Loading