Skip to content

Commit df77afb

Browse files
committed
Auto merge of rust-lang#112563 - matthiaskrgr:rollup-ebetrzi, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - rust-lang#112302 (Suggest using `ptr::null_mut` when user provided `ptr::null` to a function expecting `ptr::null_mut`) - rust-lang#112416 (Fix debug ICE for extern type with where clauses) - rust-lang#112527 (Add windows_sys type definitions for ARM32 manually) - rust-lang#112546 (new solver: extend assert to other aliases) r? `@ghost` `@rustbot` modify labels: rollup
2 parents b963a57 + 9cb785b commit df77afb

File tree

16 files changed

+219
-10
lines changed

16 files changed

+219
-10
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,12 @@ impl<'a> AstValidator<'a> {
364364
self.err_handler().emit_err(errors::BoundInContext { span, ctx });
365365
}
366366

367-
fn check_foreign_ty_genericless(&self, generics: &Generics, where_span: Span) {
367+
fn check_foreign_ty_genericless(
368+
&self,
369+
generics: &Generics,
370+
before_where_clause: &TyAliasWhereClause,
371+
after_where_clause: &TyAliasWhereClause,
372+
) {
368373
let cannot_have = |span, descr, remove_descr| {
369374
self.err_handler().emit_err(errors::ExternTypesCannotHave {
370375
span,
@@ -378,9 +383,14 @@ impl<'a> AstValidator<'a> {
378383
cannot_have(generics.span, "generic parameters", "generic parameters");
379384
}
380385

381-
if !generics.where_clause.predicates.is_empty() {
382-
cannot_have(where_span, "`where` clauses", "`where` clause");
383-
}
386+
let check_where_clause = |where_clause: &TyAliasWhereClause| {
387+
if let TyAliasWhereClause(true, where_clause_span) = where_clause {
388+
cannot_have(*where_clause_span, "`where` clauses", "`where` clause");
389+
}
390+
};
391+
392+
check_where_clause(before_where_clause);
393+
check_where_clause(after_where_clause);
384394
}
385395

386396
fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) {
@@ -1039,7 +1049,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10391049
self.check_defaultness(fi.span, *defaultness);
10401050
self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span));
10411051
self.check_type_no_bounds(bounds, "`extern` blocks");
1042-
self.check_foreign_ty_genericless(generics, where_clauses.0.1);
1052+
self.check_foreign_ty_genericless(generics, &where_clauses.0, &where_clauses.1);
10431053
self.check_foreign_item_ascii_only(fi.ident);
10441054
}
10451055
ForeignItemKind::Static(_, _, body) => {

compiler/rustc_hir_typeck/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ hir_typeck_suggest_boxing_note = for more on the distinction between the stack a
8989
9090
hir_typeck_suggest_boxing_when_appropriate = store this in the heap by calling `Box::new`
9191
92+
hir_typeck_suggest_ptr_null_mut = consider using `core::ptr::null_mut` instead
93+
9294
hir_typeck_union_pat_dotdot = `..` cannot be used in union patterns
9395
9496
hir_typeck_union_pat_multiple_fields = union patterns should have exactly one field

compiler/rustc_hir_typeck/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ pub enum SuggestBoxing {
298298
},
299299
}
300300

301+
#[derive(Subdiagnostic)]
302+
#[suggestion(
303+
hir_typeck_suggest_ptr_null_mut,
304+
applicability = "maybe-incorrect",
305+
code = "core::ptr::null_mut()"
306+
)]
307+
pub struct SuggestPtrNullMut {
308+
#[primary_span]
309+
pub span: Span,
310+
}
311+
301312
#[derive(Diagnostic)]
302313
#[diag(hir_typeck_no_associated_item, code = "E0599")]
303314
pub struct NoAssociatedItem {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::coercion::CoerceMany;
2+
use crate::errors::SuggestPtrNullMut;
23
use crate::fn_ctxt::arg_matrix::{ArgMatrix, Compatibility, Error, ExpectedIdx, ProvidedIdx};
34
use crate::gather_locals::Declaration;
45
use crate::method::MethodCallee;
@@ -814,6 +815,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
814815
);
815816
}
816817

818+
self.suggest_ptr_null_mut(
819+
expected_ty,
820+
provided_ty,
821+
provided_args[*provided_idx],
822+
&mut err,
823+
);
824+
817825
// Call out where the function is defined
818826
self.label_fn_like(
819827
&mut err,
@@ -1271,6 +1279,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12711279
err.emit();
12721280
}
12731281

1282+
fn suggest_ptr_null_mut(
1283+
&self,
1284+
expected_ty: Ty<'tcx>,
1285+
provided_ty: Ty<'tcx>,
1286+
arg: &hir::Expr<'tcx>,
1287+
err: &mut rustc_errors::DiagnosticBuilder<'tcx, ErrorGuaranteed>,
1288+
) {
1289+
if let ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Mut, .. }) = expected_ty.kind()
1290+
&& let ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Not, .. }) = provided_ty.kind()
1291+
&& let hir::ExprKind::Call(callee, _) = arg.kind
1292+
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = callee.kind
1293+
&& let Res::Def(_, def_id) = path.res
1294+
&& self.tcx.get_diagnostic_item(sym::ptr_null) == Some(def_id)
1295+
{
1296+
// The user provided `ptr::null()`, but the function expects
1297+
// `ptr::null_mut()`.
1298+
err.subdiagnostic(SuggestPtrNullMut {
1299+
span: arg.span
1300+
});
1301+
}
1302+
}
1303+
12741304
// AST fragment checking
12751305
pub(in super::super) fn check_lit(
12761306
&self,

compiler/rustc_infer/src/infer/combine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
3434
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
3535
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3636
use rustc_middle::ty::relate::{RelateResult, TypeRelation};
37-
use rustc_middle::ty::{self, AliasKind, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
37+
use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
3838
use rustc_middle::ty::{IntType, UintType};
3939
use rustc_span::DUMMY_SP;
4040

@@ -103,12 +103,12 @@ impl<'tcx> InferCtxt<'tcx> {
103103

104104
// We don't expect `TyVar` or `Fresh*` vars at this point with lazy norm.
105105
(
106-
ty::Alias(AliasKind::Projection, _),
106+
ty::Alias(..),
107107
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)),
108108
)
109109
| (
110110
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)),
111-
ty::Alias(AliasKind::Projection, _),
111+
ty::Alias(..),
112112
) if self.next_trait_solver() => {
113113
bug!()
114114
}

compiler/rustc_trait_selection/src/solve/assembly/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
320320
candidates
321321
}
322322

323-
/// If the self type of a goal is a projection, computing the relevant candidates is difficult.
323+
/// If the self type of a goal is an alias, computing the relevant candidates is difficult.
324324
///
325325
/// To deal with this, we first try to normalize the self type and add the candidates for the normalized
326326
/// self type to the list of candidates in case that succeeds. We also have to consider candidates with the

library/std/src/sys/windows/c/windows_sys.rs

+20
Original file line numberDiff line numberDiff line change
@@ -4275,3 +4275,23 @@ impl ::core::clone::Clone for XSAVE_FORMAT {
42754275
*self
42764276
}
42774277
}
4278+
// Begin of ARM32 shim
4279+
// The raw content of this file should be processed by `generate-windows-sys`
4280+
// to be merged with the generated binding. It is not supposed to be used as
4281+
// a normal Rust module.
4282+
cfg_if::cfg_if! {
4283+
if #[cfg(target_arch = "arm")] {
4284+
#[repr(C)]
4285+
pub struct WSADATA {
4286+
pub wVersion: u16,
4287+
pub wHighVersion: u16,
4288+
pub szDescription: [u8; 257],
4289+
pub szSystemStatus: [u8; 129],
4290+
pub iMaxSockets: u16,
4291+
pub iMaxUdpDg: u16,
4292+
pub lpVendorInfo: PSTR,
4293+
}
4294+
pub enum CONTEXT {}
4295+
}
4296+
}
4297+
// End of ARM32 shim
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Begin of ARM32 shim
2+
// The raw content of this file should be processed by `generate-windows-sys`
3+
// to be merged with the generated binding. It is not supposed to be used as
4+
// a normal Rust module.
5+
cfg_if::cfg_if! {
6+
if #[cfg(target_arch = "arm")] {
7+
#[repr(C)]
8+
pub struct WSADATA {
9+
pub wVersion: u16,
10+
pub wHighVersion: u16,
11+
pub szDescription: [u8; 257],
12+
pub szSystemStatus: [u8; 129],
13+
pub iMaxSockets: u16,
14+
pub iMaxUdpDg: u16,
15+
pub lpVendorInfo: PSTR,
16+
}
17+
pub enum CONTEXT {}
18+
}
19+
}
20+
// End of ARM32 shim

src/tools/generate-windows-sys/src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const PRELUDE: &str = r#"// This file is autogenerated.
1111
// ignore-tidy-filelength
1212
"#;
1313

14+
/// This is a shim for the ARM (32-bit) architecture, which is no longer supported by windows-rs.
15+
const ARM_SHIM: &str = include_str!("arm_shim.rs");
16+
1417
fn main() -> io::Result<()> {
1518
let mut path: PathBuf =
1619
std::env::args_os().nth(1).expect("a path to the rust repository is required").into();
@@ -32,6 +35,7 @@ fn main() -> io::Result<()> {
3235
let mut f = std::fs::File::create(&path)?;
3336
f.write_all(PRELUDE.as_bytes())?;
3437
f.write_all(bindings.as_bytes())?;
38+
f.write_all(ARM_SHIM.as_bytes())?;
3539

3640
Ok(())
3741
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern "C" {
2+
type Item = [T] where [T]: Sized;
3+
//~^ incorrect `type` inside `extern` block
4+
//~| `type`s inside `extern` blocks cannot have `where` clauses
5+
//~| cannot find type `T` in this scope
6+
//~| cannot find type `T` in this scope
7+
//~| extern types are experimental
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error: incorrect `type` inside `extern` block
2+
--> $DIR/issue-112363-extern-item-where-clauses-debug-ice.rs:2:10
3+
|
4+
LL | extern "C" {
5+
| ---------- `extern` blocks define existing foreign types and types inside of them cannot have a body
6+
LL | type Item = [T] where [T]: Sized;
7+
| ^^^^ --- the invalid body
8+
| |
9+
| cannot have a body
10+
|
11+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
12+
13+
error: `type`s inside `extern` blocks cannot have `where` clauses
14+
--> $DIR/issue-112363-extern-item-where-clauses-debug-ice.rs:2:21
15+
|
16+
LL | extern "C" {
17+
| ---------- `extern` block begins here
18+
LL | type Item = [T] where [T]: Sized;
19+
| ^^^^^^^^^^^^^^^^ help: remove the `where` clause
20+
|
21+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
22+
23+
error[E0412]: cannot find type `T` in this scope
24+
--> $DIR/issue-112363-extern-item-where-clauses-debug-ice.rs:2:28
25+
|
26+
LL | type Item = [T] where [T]: Sized;
27+
| ^ not found in this scope
28+
29+
error[E0412]: cannot find type `T` in this scope
30+
--> $DIR/issue-112363-extern-item-where-clauses-debug-ice.rs:2:18
31+
|
32+
LL | type Item = [T] where [T]: Sized;
33+
| ^ not found in this scope
34+
35+
error[E0658]: extern types are experimental
36+
--> $DIR/issue-112363-extern-item-where-clauses-debug-ice.rs:2:5
37+
|
38+
LL | type Item = [T] where [T]: Sized;
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40+
|
41+
= note: see issue #43467 <https://github.com/rust-lang/rust/issues/43467> for more information
42+
= help: add `#![feature(extern_types)]` to the crate attributes to enable
43+
44+
error: aborting due to 5 previous errors
45+
46+
Some errors have detailed explanations: E0412, E0658.
47+
For more information about an error, try `rustc --explain E0412`.

tests/ui/parser/foreign-ty-semantic-fail.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ extern "C" {
1515
//~^ ERROR incorrect `type` inside `extern` block
1616

1717
type E: where;
18+
//~^ ERROR `type`s inside `extern` blocks cannot have `where` clauses
1819
}

tests/ui/parser/foreign-ty-semantic-fail.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,16 @@ LL | type D = u8;
6161
|
6262
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
6363

64-
error: aborting due to 6 previous errors
64+
error: `type`s inside `extern` blocks cannot have `where` clauses
65+
--> $DIR/foreign-ty-semantic-fail.rs:17:13
66+
|
67+
LL | extern "C" {
68+
| ---------- `extern` block begins here
69+
...
70+
LL | type E: where;
71+
| ^^^^^ help: remove the `where` clause
72+
|
73+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
74+
75+
error: aborting due to 7 previous errors
6576

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
3+
#[allow(unused_imports)]
4+
use std::ptr;
5+
6+
fn expecting_null_mut(_: *mut u8) {}
7+
8+
fn main() {
9+
expecting_null_mut(core::ptr::null_mut());
10+
//~^ ERROR mismatched types
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
3+
#[allow(unused_imports)]
4+
use std::ptr;
5+
6+
fn expecting_null_mut(_: *mut u8) {}
7+
8+
fn main() {
9+
expecting_null_mut(ptr::null());
10+
//~^ ERROR mismatched types
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/ptr-null-mutability-suggestions.rs:9:24
3+
|
4+
LL | expecting_null_mut(ptr::null());
5+
| ------------------ ^^^^^^^^^^^
6+
| | |
7+
| | types differ in mutability
8+
| | help: consider using `core::ptr::null_mut` instead: `core::ptr::null_mut()`
9+
| arguments to this function are incorrect
10+
|
11+
= note: expected raw pointer `*mut u8`
12+
found raw pointer `*const _`
13+
note: function defined here
14+
--> $DIR/ptr-null-mutability-suggestions.rs:6:4
15+
|
16+
LL | fn expecting_null_mut(_: *mut u8) {}
17+
| ^^^^^^^^^^^^^^^^^^ ----------
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)