@@ -1788,7 +1788,7 @@ fn empty_arguments_object(vm: &VirtualMachine) -> PyObjectRef {
17881788
17891789#[ cfg( feature = "parser" ) ]
17901790#[ allow( clippy:: too_many_arguments) ]
1791- pub ( crate ) fn parse (
1791+ pub ( crate ) fn parse < E : From < CompileError > > (
17921792 vm : & VirtualMachine ,
17931793 source : & str ,
17941794 filename : & str ,
@@ -1800,12 +1800,13 @@ pub(crate) fn parse(
18001800 interactive : bool ,
18011801 explicit_future_features : crate :: bytecode:: CodeFlags ,
18021802 dont_imply_dedent : bool ,
1803- ) -> Result < PyObjectRef , CompileError > {
1803+ mut emit_warning : impl FnMut ( usize , char ) -> Result < ( ) , E > ,
1804+ ) -> Result < PyObjectRef , E > {
18041805 let source_file = SourceFileBuilder :: new ( filename. to_owned ( ) , source. to_owned ( ) ) . finish ( ) ;
18051806 let mut options = parser:: ParseOptions :: from ( mode) ;
18061807 let target_version = target_version. unwrap_or ( ast:: PythonVersion :: PY314 ) ;
18071808 if let Some ( error) = feature_version_syntax_error ( source, & source_file, target_version) {
1808- return Err ( error) ;
1809+ return Err ( error. into ( ) ) ;
18091810 }
18101811 options = options. with_target_version ( target_version) ;
18111812 let barry_source = rustpython_compiler:: prepare_barry_as_flufl_source (
@@ -1819,40 +1820,40 @@ pub(crate) fn parse(
18191820 && let Some ( error) =
18201821 rustpython_compiler:: single_mode_multiple_statements_error ( & source_file, & parsed)
18211822 {
1822- return Err ( error) ;
1823+ return Err ( error. into ( ) ) ;
18231824 }
18241825 let type_comment_source =
18251826 type_comments. then ( || TypeCommentSource :: new ( source, parsed. tokens ( ) ) ) ;
18261827 if let Some ( lines) = & type_comment_source
18271828 && let Some ( error) = invalid_type_comment_syntax_error ( & source_file, lines)
18281829 {
1829- return Err ( error) ;
1830+ return Err ( error. into ( ) ) ;
18301831 }
18311832 if let Err ( errors) = parsed. as_result ( ) {
18321833 let parse_error = errors[ 0 ] . clone ( ) ;
18331834 if let Some ( error) = barry_source. diagnostic ( Some ( & parse_error) , & source_file) {
1834- return Err ( error) ;
1835+ return Err ( error. into ( ) ) ;
18351836 }
18361837 let range = text_range_to_source_range ( & source_file, parse_error. location ) ;
1837- return Err ( ParseError {
1838+ return Err ( CompileError :: from ( ParseError {
18381839 error : parse_error. error ,
18391840 raw_location : parse_error. location ,
18401841 location : range. start . to_source_location ( ) ,
18411842 end_location : range. end . to_source_location ( ) ,
18421843 source_path : source_file. name ( ) . to_owned ( ) ,
18431844 is_unclosed_bracket : false ,
18441845 is_unclosed_string : false ,
1845- }
1846+ } )
18461847 . into ( ) ) ;
18471848 }
18481849 if let Some ( error) = barry_source. diagnostic ( None , & source_file) {
1849- return Err ( error) ;
1850+ return Err ( error. into ( ) ) ;
18501851 }
18511852 if dont_imply_dedent
18521853 && interactive
18531854 && let Some ( error) = rustpython_compiler:: dont_imply_dedent_source_error ( & source_file)
18541855 {
1855- return Err ( error) ;
1856+ return Err ( error. into ( ) ) ;
18561857 }
18571858
18581859 if let Some ( error) = parsed
@@ -1861,18 +1862,18 @@ pub(crate) fn parse(
18611862 . find ( |error| should_report_unsupported_syntax_error ( error) )
18621863 {
18631864 if let Some ( error) = cpython_unsupported_syntax_error ( error, source, & source_file) {
1864- return Err ( error) ;
1865+ return Err ( error. into ( ) ) ;
18651866 }
18661867 let range = text_range_to_source_range ( & source_file, error. range ( ) ) ;
1867- return Err ( ParseError {
1868+ return Err ( CompileError :: from ( ParseError {
18681869 error : parser:: ParseErrorType :: OtherError ( error. to_string ( ) ) ,
18691870 raw_location : error. range ( ) ,
18701871 location : range. start . to_source_location ( ) ,
18711872 end_location : range. end . to_source_location ( ) ,
18721873 source_path : source_file. name ( ) . to_owned ( ) ,
18731874 is_unclosed_bracket : false ,
18741875 is_unclosed_string : false ,
1875- }
1876+ } )
18761877 . into ( ) ) ;
18771878 }
18781879
@@ -1881,11 +1882,11 @@ pub(crate) fn parse(
18811882 parsed. tokens ( ) ,
18821883 vm. state . int_max_str_digits . load ( ) ,
18831884 ) {
1884- return Err ( error) ;
1885+ return Err ( error. into ( ) ) ;
18851886 }
18861887
18871888 if let Some ( error) = rustpython_compiler:: leading_byte_order_mark_error ( & source_file) {
1888- return Err ( error) ;
1889+ return Err ( error. into ( ) ) ;
18891890 }
18901891 let mut top = parsed. into_syntax ( ) ;
18911892 rustpython_compiler:: too_deeply_nested_error (
@@ -1894,18 +1895,34 @@ pub(crate) fn parse(
18941895 rustpython_compiler:: CompileOpts :: default ( ) . recursion_limit ,
18951896 ) ?;
18961897 if let Some ( error) = rustpython_compiler:: unsupported_grammar_error ( & top, & source_file) {
1897- return Err ( error) ;
1898+ return Err ( error. into ( ) ) ;
18981899 }
18991900 if let Some ( error) = ipython_escape_command_syntax_error ( & top, & source_file) {
1900- return Err ( error) ;
1901+ return Err ( error. into ( ) ) ;
19011902 }
19021903 if let Some ( error) = feature_version_ast_syntax_error ( & top, & source_file, target_version) {
1903- return Err ( error) ;
1904+ return Err ( error. into ( ) ) ;
1905+ }
1906+ #[ cfg( feature = "codegen" ) ]
1907+ let future_features = codegen:: preprocess:: checked_future_features ( & top)
1908+ . map_err ( |err| future_feature_compile_error ( & source_file, err) ) ?;
1909+ // Keep original literal spans: preprocessing can fold multiple source
1910+ // literals into one node, but each has its own lexical diagnostic.
1911+ match & top {
1912+ ast:: Mod :: Module ( module) => {
1913+ string:: emit_format_spec_warnings ( & module. body , & [ ] , & source_file, & mut emit_warning) ?;
1914+ }
1915+ ast:: Mod :: Expression ( expression) => {
1916+ string:: emit_format_spec_warnings (
1917+ & [ ] ,
1918+ core:: slice:: from_ref ( expression. body . as_ref ( ) ) ,
1919+ & source_file,
1920+ & mut emit_warning,
1921+ ) ?;
1922+ }
19041923 }
19051924 #[ cfg( feature = "codegen" ) ]
19061925 {
1907- let future_features = codegen:: preprocess:: checked_future_features ( & top)
1908- . map_err ( |err| future_feature_compile_error ( & source_file, err) ) ?;
19091926 let future_annotations = explicit_future_features
19101927 . contains ( crate :: bytecode:: CodeFlags :: FUTURE_ANNOTATIONS )
19111928 || future_features. contains ( crate :: bytecode:: CodeFlags :: FUTURE_ANNOTATIONS ) ;
@@ -1969,14 +1986,16 @@ pub(crate) fn wrap_interactive(vm: &VirtualMachine, module_obj: &PyObject) -> Py
19691986}
19701987
19711988#[ cfg( feature = "parser" ) ]
1972- pub ( crate ) fn parse_func_type (
1989+ pub ( crate ) fn parse_func_type < E : From < CompileError > > (
19731990 vm : & VirtualMachine ,
19741991 source : & str ,
19751992 filename : & str ,
19761993 optimize : u8 ,
19771994 target_version : Option < ast:: PythonVersion > ,
1978- ) -> Result < PyObjectRef , CompileError > {
1995+ mut emit_warning : impl FnMut ( usize , char ) -> Result < ( ) , E > ,
1996+ ) -> Result < PyObjectRef , E > {
19791997 let _ = optimize;
1998+ let leading_space = source. len ( ) - source. trim_start ( ) . len ( ) ;
19801999 let source = source. trim ( ) ;
19812000 let invalid_func_type = || -> CompileError {
19822001 ParseError {
@@ -2008,15 +2027,15 @@ pub(crate) fn parse_func_type(
20082027 }
20092028
20102029 let Some ( split_at) = split_at else {
2011- return Err ( ParseError {
2030+ return Err ( CompileError :: from ( ParseError {
20122031 error : parser:: ParseErrorType :: OtherError ( "invalid func_type" . to_owned ( ) ) ,
20132032 raw_location : TextRange :: default ( ) ,
20142033 location : SourceLocation :: default ( ) ,
20152034 end_location : SourceLocation :: default ( ) ,
20162035 source_path : filename. to_owned ( ) ,
20172036 is_unclosed_bracket : false ,
20182037 is_unclosed_string : false ,
2019- }
2038+ } )
20202039 . into ( ) ) ;
20212040 } ;
20222041
@@ -2029,15 +2048,15 @@ pub(crate) fn parse_func_type(
20292048 . with_target_version ( target_version. unwrap_or ( ast:: PythonVersion :: PY314 ) ) ;
20302049 let parsed = parser:: parse ( expr_src, options) . map_err ( |parse_error| {
20312050 let range = text_range_to_source_range ( & source_file, parse_error. location ) ;
2032- ParseError {
2051+ CompileError :: from ( ParseError {
20332052 error : parse_error. error ,
20342053 raw_location : parse_error. location ,
20352054 location : range. start . to_source_location ( ) ,
20362055 end_location : range. end . to_source_location ( ) ,
20372056 source_path : source_file. name ( ) . to_owned ( ) ,
20382057 is_unclosed_bracket : false ,
20392058 is_unclosed_string : false ,
2040- }
2059+ } )
20412060 } ) ?;
20422061 let ast:: Mod :: Expression ( expression) = parsed. into_syntax ( ) else {
20432062 unreachable ! ( ) ;
@@ -2046,36 +2065,39 @@ pub(crate) fn parse_func_type(
20462065 } ;
20472066
20482067 if !left. starts_with ( '(' ) || !left. ends_with ( ')' ) {
2049- return Err ( invalid_func_type ( ) ) ;
2068+ return Err ( invalid_func_type ( ) . into ( ) ) ;
20502069 }
20512070 let inner = left[ 1 ..left. len ( ) - 1 ] . trim ( ) ;
2071+ let inner_start = left. len ( ) - 1 - left[ 1 ..left. len ( ) - 1 ] . trim_start ( ) . len ( ) ;
2072+ const ARG_PREFIX : & str = "__rustpython_func_type__(" ;
2073+ let mut arg_source_file = None ;
20522074 let argtypes = if inner. is_empty ( ) {
20532075 Vec :: new ( )
20542076 } else {
20552077 if inner. ends_with ( ',' ) {
2056- return Err ( invalid_func_type ( ) ) ;
2078+ return Err ( invalid_func_type ( ) . into ( ) ) ;
20572079 }
2058- let call_source = format ! ( "__rustpython_func_type__( {inner})" ) ;
2080+ let call_source = format ! ( "{ARG_PREFIX} {inner})" ) ;
20592081 let source_file = SourceFileBuilder :: new ( filename. to_owned ( ) , call_source. clone ( ) ) . finish ( ) ;
20602082 let options = parser:: ParseOptions :: from ( parser:: Mode :: Expression )
20612083 . with_target_version ( target_version. unwrap_or ( ast:: PythonVersion :: PY314 ) ) ;
20622084 let parsed = parser:: parse ( & call_source, options) . map_err ( |parse_error| {
20632085 let range = text_range_to_source_range ( & source_file, parse_error. location ) ;
2064- ParseError {
2086+ CompileError :: from ( ParseError {
20652087 error : parse_error. error ,
20662088 raw_location : parse_error. location ,
20672089 location : range. start . to_source_location ( ) ,
20682090 end_location : range. end . to_source_location ( ) ,
20692091 source_path : source_file. name ( ) . to_owned ( ) ,
20702092 is_unclosed_bracket : false ,
20712093 is_unclosed_string : false ,
2072- }
2094+ } )
20732095 } ) ?;
20742096 let ast:: Mod :: Expression ( expression) = parsed. into_syntax ( ) else {
20752097 unreachable ! ( ) ;
20762098 } ;
20772099 let ast:: Expr :: Call ( call) = * expression. body else {
2078- return Err ( invalid_func_type ( ) ) ;
2100+ return Err ( invalid_func_type ( ) . into ( ) ) ;
20792101 } ;
20802102 let mut args = Vec :: new ( ) ;
20812103 let positional_len = call. arguments . args . len ( ) ;
@@ -2084,7 +2106,7 @@ pub(crate) fn parse_func_type(
20842106 match arg {
20852107 ast:: Expr :: Starred ( starred) => {
20862108 if seen_star || index + 1 != positional_len {
2087- return Err ( invalid_func_type ( ) ) ;
2109+ return Err ( invalid_func_type ( ) . into ( ) ) ;
20882110 }
20892111 seen_star = true ;
20902112 args. push ( * starred. value ) ;
@@ -2095,22 +2117,38 @@ pub(crate) fn parse_func_type(
20952117 let mut seen_kw_star = false ;
20962118 for keyword in call. arguments . keywords {
20972119 if keyword. arg . is_some ( ) || seen_kw_star {
2098- return Err ( invalid_func_type ( ) ) ;
2120+ return Err ( invalid_func_type ( ) . into ( ) ) ;
20992121 }
21002122 seen_kw_star = true ;
21012123 args. push ( keyword. value ) ;
21022124 }
2125+ arg_source_file = Some ( source_file) ;
21032126 args
21042127 } ;
21052128
21062129 let returns = parse_expr ( right) ?;
2130+ // Func-type nodes are parsed from separate fragments. Keep warning offsets
2131+ // in those fragments until the boundary, then map them to the original
2132+ // source without changing the AST's existing location representation.
2133+ if let Some ( source_file) = arg_source_file {
2134+ string:: emit_format_spec_warnings ( & [ ] , & argtypes, & source_file, & mut |offset, ch| {
2135+ emit_warning ( leading_space + inner_start + offset - ARG_PREFIX . len ( ) , ch)
2136+ } ) ?;
2137+ }
2138+ let return_source_file = SourceFileBuilder :: new ( filename. to_owned ( ) , right. to_owned ( ) ) . finish ( ) ;
2139+ string:: emit_format_spec_warnings (
2140+ & [ ] ,
2141+ core:: slice:: from_ref ( & returns) ,
2142+ & return_source_file,
2143+ & mut |offset, ch| emit_warning ( leading_space + source. len ( ) - right. len ( ) + offset, ch) ,
2144+ ) ?;
21072145
21082146 let func_type = ModFunctionType {
21092147 argtypes : argtypes. into_boxed_slice ( ) ,
21102148 returns,
21112149 runtime_argtypes : None ,
21122150 } ;
2113- let source_file = SourceFileBuilder :: new ( "" . to_owned ( ) , source. to_owned ( ) ) . finish ( ) ;
2151+ let source_file = SourceFileBuilder :: new ( filename . to_owned ( ) , source. to_owned ( ) ) . finish ( ) ;
21142152 Ok ( func_type. ast_to_object ( vm, & source_file) )
21152153}
21162154
@@ -2498,7 +2536,10 @@ pub(crate) fn compile(
24982536 filename : & str ,
24992537 mode : crate :: compiler:: Mode ,
25002538 mut opts : codegen:: CompileOpts ,
2539+ module : Option < & Py < PyStr > > ,
25012540) -> PyResult {
2541+ #[ cfg( not( feature = "parser" ) ) ]
2542+ let _ = module;
25022543 let text = synthetic_source_from_ast_object ( vm, & object) ?;
25032544 let source_file = SourceFileBuilder :: new ( filename. to_owned ( ) , text. clone ( ) ) . finish ( ) ;
25042545 let ast = Node :: ast_from_object ( vm, & source_file, object) ?;
@@ -2535,7 +2576,7 @@ pub(crate) fn compile(
25352576 message. into ( ) ,
25362577 fname,
25372578 location. line . get ( ) ,
2538- None ,
2579+ module . map ( |module| module . to_owned ( ) . into ( ) ) ,
25392580 vm. ctx . none ( ) ,
25402581 None ,
25412582 None ,
0 commit comments