Skip to content

fix: pass -Tp on MSVC for .cc so they are not treated as objects - #1930

Merged
NobodyXu merged 2 commits into
rust-lang:mainfrom
cestercian:cursor/fix-1877-mimalloc-object-path-4512
Sep 20, 2026
Merged

NobodyXu merged 2 commits into
rust-lang:mainfrom
cestercian:cursor/fix-1877-mimalloc-object-path-4512

Conversation

@cestercian

Copy link
Copy Markdown
Contributor

Summary

MSVC does not treat .cc as C++. With cpp(true), mimalloc 0.1.52 writes OUT_DIR/mimalloc-static.cc; without /TP, cl.exe assumes that file is an object (D9024) and never produces the -Fo output (*-mimalloc-static.o).

Pass -TP on MSVC-like compilers when cpp(true).

Fixes #1877

Test plan

  • cargo +stable test --locked --test test — 59 passed (includes msvc_cpp_cc_source_not_treated_as_object)
  • clippy on cc clean

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Is it possible to use -Tp instead, to recognise the immediate following .cc as c++ source files?

/TP could potentially break c code as C++ as slightly different rules, sometimes stricter

MSVC only recognizes .c/.cpp/.cxx as source. A .cc file such as
mimalloc 0.1.52's generated mimalloc-static.cc is assumed to be an
object (D9024), so the -Fo output is never produced.

Pass per-file -Tp immediately before each C++ source on MSVC-like
compilers. Unlike /TP, -Tp applies only to the following file and
does not compile C inputs as C++.

Fixes rust-lang#1877

Co-authored-by: Cestercian <yashafaid@gmail.com>
@cursor
cursor Bot force-pushed the cursor/fix-1877-mimalloc-object-path-4512 branch from bd4f05a to de5683e Compare September 20, 2026 08:04
@cestercian

Copy link
Copy Markdown
Contributor Author

@NobodyXu Switched to per-file -Tp immediately before the .cc path (no compiler-wide /TP / -TP), so C inputs and flag probes stay C. Regression test now asserts that shape. Head: de5683e.

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, just want the implementation to be changed as described and have the doc updated

You would probably want to update regression test to ensure C files aren't interpreted as C++, and that /Tp isn't used for clang-cl

Comment thread src/lib.rs Outdated
Comment on lines +2012 to +2024
if self.cpp && compiler.is_like_msvc() && !is_assembler_msvc {
// MSVC recognizes only `.c` / `.cpp` / `.cxx` as source. A `.cc`
// file (mimalloc 0.1.52 writes `OUT_DIR/mimalloc-static.cc`) is
// assumed to be an object unless `-Tp` forces C++ compilation
// (#1877).
cmd.arg("-Tp");
} else if compiler.supports_path_delimiter() && !is_assembler_msvc {
// #513: For `clang-cl`, separate flags/options from the input file.
// When cross-compiling macOS -> Windows, this avoids interpreting
// common `/Users/...` paths as the `/U` flag and triggering
// `-Wslash-u-filename` warning.
cmd.arg("--");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok there's a few issues we need to solve here

  • we shouldn't need to check !is_assembler_msvc for /Tp
  • -- is used for clang-cl to signal end of the options here so we cannot pass /Tp afterwards, however the good news is, we don't need it because clang-cl can recognise .cc
  • we need to pass /Tp only for .cc files

so we need to do something like this:

Suggested change
if self.cpp && compiler.is_like_msvc() && !is_assembler_msvc {
// MSVC recognizes only `.c` / `.cpp` / `.cxx` as source. A `.cc`
// file (mimalloc 0.1.52 writes `OUT_DIR/mimalloc-static.cc`) is
// assumed to be an object unless `-Tp` forces C++ compilation
// (#1877).
cmd.arg("-Tp");
} else if compiler.supports_path_delimiter() && !is_assembler_msvc {
// #513: For `clang-cl`, separate flags/options from the input file.
// When cross-compiling macOS -> Windows, this avoids interpreting
// common `/Users/...` paths as the `/U` flag and triggering
// `-Wslash-u-filename` warning.
cmd.arg("--");
}
if
self.cpp &&
src.extension == Some("cc") &&
matches!(compiler.family, ToolFamily::Msvc { clang_cl: false })
{
// MSVC recognizes only `.c` / `.cpp` / `.cxx` as source. A `.cc`
// file (mimalloc 0.1.52 writes `OUT_DIR/mimalloc-static.cc`) is
// assumed to be an object unless `-Tp` forces C++ compilation
// (#1877).
cmd.arg("-Tp");
}
if compiler.supports_path_delimiter() && !is_assembler_msvc {
// #513: For `clang-cl`, separate flags/options from the input file.
// When cross-compiling macOS -> Windows, this avoids interpreting
// common `/Users/...` paths as the `/U` flag and triggering
// `-Wslash-u-filename` warning.
cmd.arg("--");
}

@cestercian cestercian Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, narrowed it to -Tp only for .cc on MSVC (not clang-cl), and left the -- path alone

Comment thread src/lib.rs Outdated
Comment on lines +870 to +871
/// On MSVC this also passes `-Tp` immediately before each source file, so
/// inputs such as `.cc` are compiled as C++ rather than assumed to be

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// On MSVC this also passes `-Tp` immediately before each source file, so
/// inputs such as `.cc` are compiled as C++ rather than assumed to be
/// On MSVC this also passes `-Tp` immediately before each `.cc` source file to ensure that
/// they are compiled as C++ rather than assumed to be

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the cpp() docs to match

Address review: cl.exe needs -Tp only for unrecognized .cc sources.
clang-cl already recognizes .cc and uses -- as a path delimiter, so
-Tp must not be passed after --. Do not apply -Tp to .c or .cpp.

Fixes rust-lang#1877

Co-authored-by: Cestercian <yashafaid@gmail.com>
@cestercian cestercian changed the title fix: pass -TP on MSVC C++ so .cc files are not treated as objects fix: pass -Tp on MSVC for .cc so they are not treated as objects Sep 20, 2026

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

A new release is scheduled on next weekend, if you need it early, I can cut an early release

@NobodyXu
NobodyXu merged commit e2bb556 into rust-lang:main Sep 20, 2026
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compile error in mimalloc 0.1.52

2 participants