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 #78583

Closed
wants to merge 17 commits into from
Closed
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 compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ extern "C" LLVMValueRef LLVMRustDIBuilderInsertDeclareAtEnd(
return wrap(Builder->insertDeclare(
unwrap(V), unwrap<DILocalVariable>(VarInfo),
Builder->createExpression(llvm::ArrayRef<int64_t>(AddrOps, AddrOpsCount)),
DebugLoc(cast<MDNode>(DL)),
DebugLoc(cast<MDNode>(unwrap(DL))),
unwrap(InsertAtEnd)));
}

Expand Down
24 changes: 8 additions & 16 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ impl<'a> PathSource<'a> {

#[derive(Default)]
struct DiagnosticMetadata<'ast> {
/// The current trait's associated types' ident, used for diagnostic suggestions.
current_trait_assoc_types: Vec<Ident>,
/// The current trait's associated items' ident, used for diagnostic suggestions.
current_trait_assoc_items: Option<&'ast [P<AssocItem>]>,

/// The current self type if inside an impl (used for better errors).
current_self_type: Option<Ty>,
Expand Down Expand Up @@ -1157,26 +1157,18 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
result
}

/// When evaluating a `trait` use its associated types' idents for suggestionsa in E0412.
/// When evaluating a `trait` use its associated types' idents for suggestions in E0412.
fn with_trait_items<T>(
&mut self,
trait_items: &Vec<P<AssocItem>>,
trait_items: &'ast Vec<P<AssocItem>>,
f: impl FnOnce(&mut Self) -> T,
) -> T {
let trait_assoc_types = replace(
&mut self.diagnostic_metadata.current_trait_assoc_types,
trait_items
.iter()
.filter_map(|item| match &item.kind {
AssocItemKind::TyAlias(_, _, bounds, _) if bounds.is_empty() => {
Some(item.ident)
}
_ => None,
})
.collect(),
let trait_assoc_items = replace(
&mut self.diagnostic_metadata.current_trait_assoc_items,
Some(&trait_items[..]),
);
let result = f(self);
self.diagnostic_metadata.current_trait_assoc_types = trait_assoc_types;
self.diagnostic_metadata.current_trait_assoc_items = trait_assoc_items;
result
}

Expand Down
58 changes: 47 additions & 11 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,21 @@ type Res = def::Res<ast::NodeId>;
enum AssocSuggestion {
Field,
MethodWithSelf,
AssocItem,
AssocFn,
AssocType,
AssocConst,
}

impl AssocSuggestion {
fn action(&self) -> &'static str {
match self {
AssocSuggestion::Field => "use the available field",
AssocSuggestion::MethodWithSelf => "call the method with the fully-qualified path",
AssocSuggestion::AssocFn => "call the associated function",
AssocSuggestion::AssocConst => "use the associated `const`",
AssocSuggestion::AssocType => "use the associated type",
}
}
}

crate enum MissingLifetimeSpot<'tcx> {
Expand Down Expand Up @@ -386,15 +400,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
AssocSuggestion::MethodWithSelf if self_is_available => {
err.span_suggestion(
span,
"try",
"you might have meant to call the method",
format!("self.{}", path_str),
Applicability::MachineApplicable,
);
}
AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => {
AssocSuggestion::MethodWithSelf
| AssocSuggestion::AssocFn
| AssocSuggestion::AssocConst
| AssocSuggestion::AssocType => {
err.span_suggestion(
span,
"try",
&format!("you might have meant to {}", candidate.action()),
format!("Self::{}", path_str),
Applicability::MachineApplicable,
);
Expand Down Expand Up @@ -1062,9 +1079,19 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}
}

for assoc_type_ident in &self.diagnostic_metadata.current_trait_assoc_types {
if *assoc_type_ident == ident {
return Some(AssocSuggestion::AssocItem);
if let Some(items) = self.diagnostic_metadata.current_trait_assoc_items {
for assoc_item in &items[..] {
if assoc_item.ident == ident {
return Some(match &assoc_item.kind {
ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
ast::AssocItemKind::Fn(_, sig, ..) if sig.decl.has_self() => {
AssocSuggestion::MethodWithSelf
}
ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn,
ast::AssocItemKind::TyAlias(..) => AssocSuggestion::AssocType,
ast::AssocItemKind::MacCall(_) => continue,
});
}
}
}

Expand All @@ -1080,11 +1107,20 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
) {
let res = binding.res();
if filter_fn(res) {
return Some(if self.r.has_self.contains(&res.def_id()) {
AssocSuggestion::MethodWithSelf
if self.r.has_self.contains(&res.def_id()) {
return Some(AssocSuggestion::MethodWithSelf);
} else {
AssocSuggestion::AssocItem
});
match res {
Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn),
Res::Def(DefKind::AssocConst, _) => {
return Some(AssocSuggestion::AssocConst);
}
Res::Def(DefKind::AssocTy, _) => {
return Some(AssocSuggestion::AssocType);
}
_ => {}
}
}
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions library/std/src/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,12 +719,13 @@ impl f32 {
/// # Examples
///
/// ```
/// let x = 6.0f32;
/// let x = 1e-8_f32;
///
/// // e^(ln(6)) - 1
/// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
/// // for very small x, e^x is approximately 1 + x + x^2 / 2
/// let approx = x + x * x / 2.0;
/// let abs_difference = (x.exp_m1() - approx).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference < 1e-10);
/// ```
#[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -739,12 +740,13 @@ impl f32 {
/// # Examples
///
/// ```
/// let x = std::f32::consts::E - 1.0;
/// let x = 1e-8_f32;
///
/// // ln(1 + (e - 1)) == ln(e) == 1
/// let abs_difference = (x.ln_1p() - 1.0).abs();
/// // for very small x, ln(1 + x) is approximately x - x^2 / 2
/// let approx = x - x * x / 2.0;
/// let abs_difference = (x.ln_1p() - approx).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference < 1e-10);
/// ```
#[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
18 changes: 10 additions & 8 deletions library/std/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,13 @@ impl f64 {
/// # Examples
///
/// ```
/// let x = 7.0_f64;
/// let x = 1e-16_f64;
///
/// // e^(ln(7)) - 1
/// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
/// // for very small x, e^x is approximately 1 + x + x^2 / 2
/// let approx = x + x * x / 2.0;
/// let abs_difference = (x.exp_m1() - approx).abs();
///
/// assert!(abs_difference < 1e-10);
/// assert!(abs_difference < 1e-20);
/// ```
#[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -741,12 +742,13 @@ impl f64 {
/// # Examples
///
/// ```
/// let x = std::f64::consts::E - 1.0;
/// let x = 1e-16_f64;
///
/// // ln(1 + (e - 1)) == ln(e) == 1
/// let abs_difference = (x.ln_1p() - 1.0).abs();
/// // for very small x, ln(1 + x) is approximately x - x^2 / 2
/// let approx = x - x * x / 2.0;
/// let abs_difference = (x.ln_1p() - approx).abs();
///
/// assert!(abs_difference < 1e-10);
/// assert!(abs_difference < 1e-20);
/// ```
#[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
14 changes: 14 additions & 0 deletions library/std/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ pub use crate::panicking::{set_hook, take_hook};
#[stable(feature = "panic_hooks", since = "1.10.0")]
pub use core::panic::{Location, PanicInfo};

/// Panic the current thread with the given message as the panic payload.
///
/// The message can be of any (`Any + Send`) type, not just strings.
///
/// The message is wrapped in a `Box<'static + Any + Send>`, which can be
/// accessed later using [`PanicInfo::payload`].
///
/// See the [`panic!`] macro for more information about panicking.
#[unstable(feature = "panic_any", issue = "78500")]
#[inline]
pub fn panic_any<M: Any + Send>(msg: M) -> ! {
crate::panicking::begin_panic(msg);
}

/// A marker trait which represents "panic safe" types in Rust.
///
/// This trait is implemented by default for many types and behaves similarly in
Expand Down
38 changes: 32 additions & 6 deletions library/std/src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,18 @@ impl Socket {
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
unsafe {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
// On Linux we pass the SOCK_CLOEXEC flag to atomically create
// the socket and set it as CLOEXEC, added in 2.6.27.
if #[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "opensbd",
))] {
// On platforms that support it we pass the SOCK_CLOEXEC
// flag to atomically create the socket and set it as
// CLOEXEC. On Linux this was added in 2.6.27.
let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
Ok(Socket(FileDesc::new(fd)))
} else {
Expand All @@ -83,7 +92,15 @@ impl Socket {
let mut fds = [0, 0];

cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
if #[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "opensbd",
))] {
// Like above, set cloexec atomically
cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?;
Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1]))))
Expand Down Expand Up @@ -174,9 +191,18 @@ impl Socket {
pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
// Unfortunately the only known way right now to accept a socket and
// atomically set the CLOEXEC flag is to use the `accept4` syscall on
// Linux. This was added in 2.6.28, glibc 2.10 and musl 0.9.5.
// platforms that support it. On Linux, this was added in 2.6.28,
// glibc 2.10 and musl 0.9.5.
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
if #[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "opensbd",
))] {
let fd = cvt_r(|| unsafe {
libc::accept4(self.0.raw(), storage, len, libc::SOCK_CLOEXEC)
})?;
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/meta/auxiliary/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Check that aux builds can also use rustc-env, but environment is configured
// separately from the main test case.
//
// rustc-env:COMPILETEST_BAR=bar

pub fn test() {
assert_eq!(option_env!("COMPILETEST_FOO"), None);
assert_eq!(env!("COMPILETEST_BAR"), "bar");
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/meta-expected-error-correct-rev.rs:7:18
--> $DIR/expected-error-correct-rev.rs:7:18
|
LL | let x: u32 = 22_usize;
| --- ^^^^^^^^ expected `u32`, found `usize`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// revisions: a

// Counterpart to `meta-expected-error-wrong-rev.rs`
// Counterpart to `expected-error-wrong-rev.rs`

#[cfg(a)]
fn foo() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Meta test for compiletest: check that when we give the right error
// patterns, the test passes. See all `meta-revision-bad.rs`.
// patterns, the test passes. See all `revision-bad.rs`.

// run-fail
// revisions: foo bar
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/meta/rustc-env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Compiletest meta test checking that rustc-env and unset-rustc-env directives
// can be used to configure environment for rustc.
//
// run-pass
// aux-build:env.rs
// rustc-env:COMPILETEST_FOO=foo
//
// An environment variable that is likely to be set, but should be safe to unset.
// unset-rustc-env:PWD

extern crate env;

fn main() {
assert_eq!(env!("COMPILETEST_FOO"), "foo");
assert_eq!(option_env!("COMPILETEST_BAR"), None);
assert_eq!(option_env!("PWD"), None);
env::test();
}
32 changes: 32 additions & 0 deletions src/test/ui/resolve/associated-fn-called-as-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
struct S;
impl Foo for S {
fn parse(s:&str) {
for c in s.chars() {
match c {
'0'..='9' => collect_primary(&c), //~ ERROR cannot find function `collect_primary`
//~^ HELP you might have meant to call the associated function
'+' | '-' => println!("We got a sign: {}", c),
_ => println!("Not a number!")
}
}
}
}
trait Foo {
fn collect_primary(ch:&char) { }
fn parse(s:&str);
}
trait Bar {
fn collect_primary(ch:&char) { }
fn parse(s:&str) {
for c in s.chars() {
match c {
'0'..='9' => collect_primary(&c), //~ ERROR cannot find function `collect_primary`
//~^ HELP you might have meant to call the associated function
'+' | '-' => println!("We got a sign: {}", c),
_ => println!("Not a number!")
}
}
}
}

fn main() {}
15 changes: 15 additions & 0 deletions src/test/ui/resolve/associated-fn-called-as-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0425]: cannot find function `collect_primary` in this scope
--> $DIR/associated-fn-called-as-fn.rs:6:30
|
LL | '0'..='9' => collect_primary(&c),
| ^^^^^^^^^^^^^^^ help: you might have meant to call the associated function: `Self::collect_primary`

error[E0425]: cannot find function `collect_primary` in this scope
--> $DIR/associated-fn-called-as-fn.rs:23:30
|
LL | '0'..='9' => collect_primary(&c),
| ^^^^^^^^^^^^^^^ help: you might have meant to call the associated function: `Self::collect_primary`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0425`.
Loading